Reading the Source of Ruby on Rails
I have decided to read the whole source of Ruby on Rails and make notes as I work my way through it. I have used the framework for far too long without doing such an obvious step and have just accepted the “magic” of how things work. Well, I’d like to see how the magic is made, and perhaps in so doing, I can become more useful as a Rubyist. These posts are going to read more like a journal, as that’s precisely what they are.
First step, setup an environment in which to really dig into Rails, in this case, I’m going to use the Rails-Dev-Box which is recommended for contributors.
λ: Development $> git clone https://github.com/rails/rails-dev-box.git # stuff happens λ: Development $> cd rails-dev-box λ: rails-dev-box - master $> vagrant up # vagrant and puppet stuff happens
Then I go to GitHub and fork the rails repo into my account and continue:
λ: rails-dev-box - master $> ls README.md Vagrantfile puppet λ: rails-dev-box - master $> git clone [email protected]:zhubert/rails.git # much downloading
Once that is complete, I have a rails directory that my Vagrant box can access and which is readily accessible from my host machine for code reading. Of course, I don’t need the Vagrant setup for just reading, but I’d like to be able to experiment with Rails master in a nicely controlled environment, so I’ll sink the extra 3 minutes to get it setup.
I open up my trusty editor and realize I need to pick a starting place to my code read through.
As you can see, Rails is a nicely structured code base, in fact, it’s organized like a bunch of gems (how Ruby like!). In the very top level there are some helpful Markdown formatted files as well as the entire source for the super awesome Guides. If you haven’t read the Guides then I would definitely recommend doing that first. Digging into the source is a good thing to do once you have a good mental image for the whole system. Moving on…
To me, it feels like a good idea to start with the extensions that have been added, so I’m not confused by those later on, so I jump right into “active_support/core_ext”. This could be a sub-par approach, but I won’t know until I’ve read through everything and I want to just get going.
First impression: this is really well commented code. I’m excited to dig in. I start in “active_support/core_ext/array/access.rb”
class Array # Returns the tail of the array from +position+. # # %w( a b c d ).from(0) # => ["a", "b", "c", "d"] # %w( a b c d ).from(2) # => ["c", "d"] # %w( a b c d ).from(10) # =>  # %w().from(0) # =>  def from(position) self[position, length] ||  end
Just the right amount of fore matter, and a good sense of humor to boot:
# Equal to <tt>self</tt>. Also known as accessing "the reddit". def forty_two self end
That’s all for now, off to enjoy Saturday sun (a rarity in Seattle)!