Forums Ruby

Standing on the shoulders of giants

Subscribe to Standing on the shoulders of giants 0 post(s), 1 voice

 
Avatar Scott Woods Administrator 41 post(s)

Anthony reminded me to post more information about the code reading talk that I went to at RailsConf ‘07. The name of the talk was “Standing on the shoulders of giants”, by Adam Keys. The talk was one of the best that I went to there, but of course that means that the slides by themselves aren’t particularly helpful (although they are funny sometimes). Here’s his blog post about the talk:

http://mvm.therealadam.com/articles/2007/05/23/...

and here’s a PDF of the slides:

http://therealadam.com/presentations/standing_o…

To summarize, here are the projects that he recommends as outstanding examples of Ruby and Rails programming, along with my notes from the talk:

  • xmlrpc4r, by Michael Neumann. Recommended as a good start. It’s in the Ruby standard library, so it’s already on your machine, under /usr/lib/ruby/1.8/xmlrpc/ (leave off the ”/usr/lib” on windows).
  • Beast, by Josh Goebel and Rick Olson. Good compact code, good idioms. Start in config/routes.rb. The use of before_create to make the first user to sign in an admin is cool too.
  • Rake, by Jim Weirich. Great example of overloading the Ruby programming language (metaprogramming).
  • acts_as_versioned, by Rick Olson. Succinct. Also a good start. This is what we’ll use when we add wiki pages to statecollegeruby.org
  • RJS, by Marcel Molina Jr. and Sam Stephenson. Check out the code for how the magic page variable is implemented.
  • The Ruby Amazon S3 library, by Marcel Molina Jr. (make sure you get the one that’s by him, and not one of the other Ruby implementations)
  • activesupport, in the rails core
  • Capistrano, by Jamis Buck (I’d recommend 2.0 over 1.x)
  • Dwemthy’s Array by Why The Lucky Stiff, for a totally mind-blowing example of metaprogramming. Plus the surrounding material is totally whacked. Why is a genius mad scientist, he is.

When trying to track down behavior, look at the URL, and then use config/routes.rb to find the controller. Then grok the controller filters. Then grok the controller actions. Then start to grok the methods and models called by the actions.

Pre-reading homework on a new application:
  • Check the controllers/application.rb and views/helpers/application_helper.rb. This is application-wide code that will be called from elsewhere.
  • Check for observers, validations, and callbacks in models. These make for non-linear control flow during an action.
  • Check the code that’s in lib/ and vendor/plugins/

For workflow, when you find a method to investigate while reading code, use your editor to leave a bookmark there, then go “dive down the rabbit hole” and then use the bookmark to pop back up again so that you can come back to where you were.

When trying to figure out method declaration and dispatch, there are a few possibilities for how something could have been implemented:
  • normally defined methods (using “def”)
  • method_missing
  • [:foo, :bar].each { |n| define_method(n) ... }
  • total craziness, e.g. strange routes

Don’t forget about the Ruby magic global variable $_, which some methods use if they’re not supplied with an argument (e.g. shift). This isn’t used very frequently, but you’ll see it sometimes, and it’s confusing when you have methods that seem to be operating on nothing. It’s used in Dwemthy’s Array, for example.

Make sure you read Err the Blog, and The Rails Way (they’re both in the feeds that I posted earlier).

Check out activesupport in the rails source code (under /usr/lib/ruby/gems/1.8/gems/activesupport…) for many tiny examples of extending the Ruby language. This is where they define things that let you say things like:

3.gigabytes  # => 3221225472
5.hours.ago  # => Wed Aug 22 10:10:17 -0400 2007
Person.find(:first).collect(&:comments).sort_by(&:created_at)

The last one would return all of the comments from the first person, sorted by creation time. The activesupport addition is the “&:” notation, also called “symbol to proc”.

Be sure to post when you find something really cool, or really confusing!

Forums Ruby