Preparing for a Rails Tour - Metaprogramming in Rails - Metaprogramming Ruby 2: Program Like the Ruby Pros (2014)

Metaprogramming Ruby 2: Program Like the Ruby Pros (2014)

Part 2. Metaprogramming in Rails

Chapter 8. Preparing for a Rails Tour

Good artists copy, great artists steal.

Pablo Picasso

In the first part of this book, you spent a week brushing elbows with another coder and making your way through the internals of Ruby. You also filled your toolbox with magic metaprogramming tricks, such as Dynamic Methods (Dynamic Method) and Class Macros (Class Macro).

So, you’ve got the know-how and the tools. But now you’re wondering how to combine them into real-life code. How can you keep your Open Classes (Open Class) under control? When should you use a Ghost Method (Ghost Method) rather than a Dynamic Method (Dynamic Method)? How do you test your Class Macros (Class Macro)? To answer these kinds of questions, you need more than knowledge and tools. You need experience.

You can’t get experience simply by reading a book, but you can get a lot of value out of looking at the work of experienced coders. The short chapters in this second part of the book take you on a tour through the source code of Ruby on Rails (or just “Rails,” for short), the quintessential Ruby project. Rails’ code uses metaprogramming at every step and is often more complex than any code you’ve seen so far in this book. Because of that complexity, it’s a great example of both the power of metaprogramming and its potential dangers.

Rather than an exhaustive exploration of Rails, this tour is like a sightseeing excursion on one of those open-air, double-decker buses. I’ll trace a few scenic routes through the Rails source code, and in the process show you how some of the best Ruby programmers apply metaprogramming spells to solve real-life problems. But first, let’s talk about Rails itself.

Ruby on Rails

Chances are, you already know that Rails is a framework for developing database-backed web applications in Ruby. Rails is so popular that many people get into Ruby just so that they can use Rails.

Even if you don’t know much about Rails and its features, you can still follow along on this tour. We’ll focus on the Rails source code, not on the features. Whenever features are important to understand the source code, I’ll take the time to demonstrate them. Although you don’t have to, you might want to get a quick introduction to Rails on its official site[9] if you’re completely new to it.

While touring the Rails source code, I’ll show you the snippets of code I want to focus on. However, you might also want to keep the source code handy to explore it on your own. To do that, you need to install Rails.

Installing Rails

Because Rails is always evolving, it’s quite possible that the source code will have changed significantly by the time you read this chapter. Luckily, you can easily install the same version of Rails that I used to write this book, by typing gem install rails -v 4.1.0.

Some of the next few chapters also discuss code from a much older version of Rails, to show you how Rails’ source code has evolved over time. If you wish, you can install this older version alongside the more recent one, by typing gem install rails -v 2.3.2.

Running the commands above installs all the gems that make up Rails 4.1.0 and 2.3.2. The rails gem just contains helpers, such as code generators and Rake tasks, as well as the glue code that binds together the other gems. Those other gems are the ones that do the real work. Three of the most important ones are activerecord (which maps application objects to database tables), actionpack (which deals with the “web” part of the web framework), and activesupport (which contains utilities for generic problems, such as time calculations and logging).

The Rails Source Code

When referring to a specific source file, I’ll give you the file’s path inside the system’s gems directory, such as gems/activerecord-4.1.0/lib/active_record.rb. If you want to explore on your own, you can use RubyGem’s unpack command to access Rails’ entire source code with a minimum of fuss. For example, gem unpack activerecord -v=4.1.0 will copy the entire distribution of Active Record 4.1.0 to the current directory.

As of version 4, Rails and its core libraries contain almost 170,000 lines of code (including white lines and comments). You can cram a lot of information into just a few lines of Ruby code—let alone hundreds of thousands. Also, you can barely find a Rails source file that doesn’t make heavy use of metaprogramming spells and other sophisticated idioms and techniques. All things considered, the Rails source code contains enough information to be intimidating.

These challenges shouldn’t stop you from browsing through this wonderful code. The Rails source code can be daunting, but it’s also chock full of interesting metaprogramming tricks. Start slowly, don’t get discouraged as you piece together the basics, and soon you might enter the growing list of Rails contributors.

Also, don’t forget the unit tests. When you’re confronted with a confusing piece of code, reach for its tests and find out how it’s supposed to be used. Once you understand their intention, most perplexing lines of code will suddenly make sense.

Now you have the Rails source code and the tools you need to explore it. In the next chapter, we’ll dive into the first stop on our tour: a quick look at Active Record, the most iconic of the Rails components.

Footnotes

[9]

http://rubyonrails.org