practicingruby / rbp-book Goto Github PK
View Code? Open in Web Editor NEWRuby Best Practices, by Gregory Brown
Home Page: http://rubybestpractices.com
Ruby Best Practices, by Gregory Brown
Home Page: http://rubybestpractices.com
Welcome to the open source home of the "Ruby Best Practices" book. Here you'll find the original manuscript along with the production files that were used to generate the print version of the book. If instead you were looking for a free PDF download of the book, you can find it here: http://sandal.github.com/rbp-book/pdfs/rbp_1-0.pdf Or, if you wanted to kill trees and give me some money: http://oreilly.com/catalog/9780596523015/ http://www.amazon.com/gp/product/0596523009/ But assuming you are here for the source, check the brief description below. == Files manuscript/unmerged contains asciidoc sources that have not been updated to reflect copyediting. When I get around to it, manuscript/updated will contain the updated files. Once a file is updated, I will accept patches against it for fixes and modifications. oreilly_final/ contains the production files that were used to generate this book. Right now it's a bit limited, just one giant docbook file and some figs. We may be able to break it down by chapter later, but we may not necessarily need it. If you are wondering about code samples, they are currently at: http://github.com/sandal/rbp I plan to merge them here sooner or later, and extract more from the original manuscript. I sort of got lazy there. == Contributing / Using Content Right now, I need to go through the painstaking process of merging copyeditor changes into my asciidoc manuscript, and then setup the build toolchain again in a way that's easy enough for contributors to access. But for those who wish to fork and experiment on their own, all content here is hereby released under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 license ( http://creativecommons.org/licenses/by-nc-sa/3.0/ ). If you have any questions about legal usage, contact me, and I'll talk with O'Reilly. gregory.t.brown at gmail.com
when visiting the site without the "www." part, it shows an "under construction page". This is confusing, it would be better to have the same content as "www.rubybestpractices.com"
(Reported by DrErnie on RBP blog)
There appears to be a typo in the last paragraph:
"the myriad collection languages that people are comfortable with."
"collection OF languages", perhaps?
in is an operator, therefore not a valid identifier
On page 137 of Chapter 5 should the self#authenticate method be changed from this:
def self.authenticate(username, password)
user = new(username, password)
user.authenticate(password)
rescue Net::LDAP::LdapError => e
ActiveRecord::Base.logger.debug "!!! LDAP Error: #{e.message} !!!"
false
end
To this since the initialize method only takes one parameter?
def self.authenticate(username, password)
user = new(username) # <= Remove the password parameter
user.authenticate(password)
rescue Net::LDAP::LdapError => e
ActiveRecord::Base.logger.debug "!!! LDAP Error: #{e.message} !!!"
false
end
I'm not sure how those "pseudo-keyword arguments" work in the RBP code (ex "Ruby’s Secret Power: Flexible Argument Processing" pg 5):
# Pseudo-keyword arguments
def story(options)
"#{options[:person]} went to town, riding on a #{options[:animal]}"
end
The example in the book is this:
story(animal: "Tiger", person: "Yankee Doodle")
# => "Yankee Doodle went to town, riding on a Tiger"
But if I enter that code into IRB:
irb(main):012:0> story(animal: "Tiger", person: "Yankee Doodle")
SyntaxError: compile error
(irb):12: syntax error, unexpected ':', expecting ')'
story(animal: "Tiger", person: "Yankee Doodle")
^
(irb):12: syntax error, unexpected ',', expecting $end
story(animal: "Tiger", person: "Yankee Doodle")
^
from (irb):12
from :0
The example works if I use a hash (as I had expected):
irb(main):013:0> story(:animal => "Tiger", :person => "Yankee Doodle")
=> "Yankee Doodle went to town, riding on a Tiger"
So I'm wondering: how did the book's JSON/Python-esque code work? Is it running on Ruby 1.9 (1.8.7 here), or is there some hack I'm missing?
Method calls are parenthesized irregularly. For example, on page 21: In the ["y", "Y", "YeS", "YES", "yes"].each do |y|
block, the calls to expect_output
and assert
are not parenthesized while provide_input
is.
Chapter 4 / Regular Expressions / Don't Work Too Hard introduces the section with the following code:
["James Gray", "James gray", "james gray", "james Gray"].all? { |e| ?> e.match(/James|james Gray|gray/) }
The regex in this example (/James|james Gray|gray/
) will match any string containing "James", or "james Gray", or "gray", which is probably not what you intended. That is,
>> "gray".match(/James|james Gray|gray/) && true => true >> "Gray".match(/James|james Gray|gray/) && true => nil
Suggest either /(James|james) (Gray|gray)/
or /(?:James|james) (?:Gray|gray)
, or note that finding the bug is an exercise for the reader.
PDF link in README is invalid. NO such GitHub site.
http://sandal.github.com/rbp-book/pdfs/rbp_1-0.pdf
on pages 36 and 37 of rbp_1-0.pdf, the tests say:
must "return false when parsing #{y}" do
where they should say
must "return true when parsing #{y}" do
that is, "true" instead of "false"
Hi,
Was reading your book and fascinated by the Fibonacci example with Memoization. Tried it out and it doesn't work.
Attached is the code used and the error code
def fib(n)
@series[n] ||= fib(n-1) + fib(n-2)
end
p fib(50)
NoMethodError: undefined method `[]' for nil:NilClass
method fib in test2.rb at line 2
at top level in test2.rb at line 5
page 46, the TCPServer doesn't work as expected. To handle multiple lines of input, it needs to loop on the session.gets, and it would be good form to close the session afterwards.
Just for fun, I also added the ability to close the connection and shut down the server from the client:
require 'socket' class Server def initialize(port=3333) @server = TCPServer.new('127.0.0.1',port) @handlers = {} end def handle(pattern, &block) @handlers[pattern] = block end def run @shutdown = false while !@shutdown and session = @server.accept @running = true while @running and msg = session.gets match = nil @handlers.each do |pattern,block| if match = msg.match(pattern) instance_eval &block break session.puts(block.call(match)) end end unless match session.puts "Server received unknown message: #{msg}" end end session.close end end end server = Server.new server.handle(/hello/i) { "Hello from server at #{Time.now}" } server.handle(/goodbye/i) { @running = false ; "Goodbye from server at #{Time.now}" } server.handle(/name is (\w+)/) { |m| "Nice to meet you #{m[1]}!" } server.handle(/shutdown/) {@shutdown = true ; "Shutting down now, Goodbye"} server.run
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.