Search code examples
ruby-on-railsopen-source

Getting started with learning the Rails source


I've been using Ruby on Rails for many projects lately, and I thought it would be interesting to take a look at the Rails source and really see how things operate underneath. I think it'd be a great learning experience and would probably enhance the way I code Rails apps all the more.

Does anyone have any tips on how to get started? And where within the Rails source does an application begin to be executed? Perhaps if I started there, I could see how everything is loaded and works in general.


Solution

  • I think it'd be a great learning experience and would probably enhance the way I code Rails apps all the more.

    This is a great idea!

    The first place you should start from is the Rails source on GitHub (here the branch 2.3). If you are using Rails, you are probably familiar with the fact that Rails itself is composed by a few different Gems: ActiveRecord, ActiveSupport, ...

    Jumping immediately into the code cannot be that simple. I would suggest you two alternative ways to start digging into Rails codebase:

    • take the habit, any time you use a method, an helper or a Rails command to jump to lookup the method in the source code and read it. Try to understand its context, how it works and which methods/libraries it uses. Then, each time the method A uses a method B, start to walk back and lookup method B. Set a limit to the number of reverse lookup, for example 2 upper levels so that you won't end up looking up the entire framework starting from the link_to helper.

    • instead of starting from the top of the repository, choose the library you are most familiar with. If you don't have any preference, start from ActiveSupport. ActiveSupport is the Rails toolkit. It provides tons of extensions you can use in your Rails code and even in your Ruby programs.

    It will take a while before you'll be able to put together all the information and understand how a single Rails application works, but it is definitely worth the effort.

    As a side note, a few month ago I started a series called Inside Ruby on Rails. You might want to give it a look.