Giter Club home page Giter Club logo

rbp-book's Introduction

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


rbp-book's People

Contributors

practicingruby avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rbp-book's Issues

Typo in last page of CH 7

(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?

p137 - Extra parameter in method call?

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

Pseudo-Keyword Arguments

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?

bug in Regexp in Chapter 4

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.

Fibonacci example doesn't work

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

TCPserver issue in example

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 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.