Metaprogramming lets you program more expressively. This makes your code easier to write and easier to maintain and extend. Learn both the hows and whys of metaprogramming Ruby from Dave Thomas, one of the most experienced Ruby programmers in the western world.
The Ruby Object Model and Metaprogramming
by Dave Thomas
About This Book
Initially, metaprogramming Ruby can seem really difficult. There are all these unfamiliar terms: singletons, self, metaclasses, and so on. And things never seem to work quite right the first time—Hmm… maybe it’ll work if I change
Well, the good news is that it really isn’t that complex. You just have to understand the underlying model, and everything falls into place. Dave Thomas has been digging into that model for the last ten years, and has a fresh (and simple) way of looking at it. Now you’ll be able to see why metaprogramming works, and how to do it for yourself.
It doesn’t matter if you’ve been programming Ruby for a month or for five years. We bet that you’ll learn plenty new in each of these episodes, as Dave digs into both the how and the why of the Ruby object model and metaprogramming.
Dave talks about the series in this free preview.
(3 mins, QuickTime | Ogg)
Watch some free extracts from Episodes 2 and 3.
(9 mins, QuickTime | Ogg)
Read the reviews .
Contents & Extracts
- Episode 1: Objects and Classes (29 minutes)
Just what is a Ruby object, and how can Ruby move you away from class-oriented development and back to object-oriented development? Learn about:
- the internals of objects,
- how classes really aren’t anything special,
selfdoes, and the two ways it can get changed
- the method call mantra: “one to the right, then up,”
- singleton methods and ghost classes,
- why class methods don’t exist,
- how classes get their names,
- how the concept of the current class interacts with
- Episode 2: Sharing Behavior (39 minutes)
One of the primary goals of good design is to put the right behavior in the right place without duplication. We’ll see three ways Ruby excels at this:
- Using prototype-based programming, where you can create hierarchies of objects with controlled sharing of both state and behavior, and without a class definition in sight.
- using inheritance (but you can only watch this section after you’ve watched the included Public Safety announcement on why inheritance is normally a bad idea). We’ll also look at what happens when you do
class<< object (and why it’s nothing whatsoever to do with inheritance).
- using modules and mixins, the sweet spot of Ruby programming. How to using
extend, and how to create modules that mix in both instance and class methods.
Episodes 1 and 2 are the foundation for the rest of the screencast.
- Episode 3: Dynamic Code (31 minutes)
Metaprogramming is sometimes defined as “writing code that writes code.” Here we’ll see just how to do that in Ruby.
- Blocks and the two kinds of block-objects
- How bindings capture execution context
- How blocks can act as closures, and why that’s vital to metaprogramming
- Writing methods that define other methods using nested
- Writing your own accessor methods
- Episode 4: instance_eval and class_eval (29 minutes)
Two of the workhorse methods of metaprogramming are
class_eval. They allow you to execute chunks of code dynamically. Here we’ll see how to use them.
- When to use one versus the other
- Breaking down barriers
- Creating methods on the fly, but without using closures
- Defining stuff in classes given a class object
- Writing DSLs in a block structure
- Episode 5: Nine Examples of Metaprogramming (36 minutes)
Here’s where all the theory and practice comes together! We’ll take a simple problem and find nine different ways of attacking it using metaprogramming techniques.
- Adding behavior directly inside the class
- Using subclassing to add behavior
- Subclassing with a generator using
- Using a ghost class
- Ghost class with a generator using
- Rewrite the method with
- Rewrite using a module
- Rewrite using
- Writing a DSL in a block
- Episode 6: Some Hook Methods (35 minutes)
Ruby hook methods are a way for your application to hook itself into the execution of the Ruby interpreter. Using hook methods is crucial for some kinds of metaprogramming, and they can make your code more flexible. In this episode, we’ll see how to use two powerful hook methods:
- Overriding hook methods to intercept and deal with certain Ruby interpreter events during the lifetime of your application
- Decoupling code using
inheritedto keep track of subclasses
- Implementing enumerated types with
const_missingto autoload classes based on the names of files
const_missingin both global and localized situations
- How to chain an overridden hook method to its original hook method
const_setto define constants
- Two practical uses for hook methods
- Episode 7: More Hook Methods (53 minutes)
We’ll pick up where we left off in the last episode by looking at two more Ruby hook methods:
method_added. But we’ll also take it a step further. We’ll use these hook methods to develop a metaprogramming library that traces the execution of a Ruby program. Along the way we’ll see all the various subtle (and important!) things you need to think about when you’re trying to write a general-purpose metaprogramming library.
includedto intercept when a module is included in a class, and use it to set up another hook method in a different context
method_addedto track when a new method is defined on a class, and trace the method’s execution
- Refactoring the tracing to support blocks
- Using method objects to bypass naming issues
- Adding tracing to methods that have already been added
- Suppressing tracing for certain methods
Thread.currentto define thread-local variables
- Differences between Ruby 1.8 and 1.9
- Practical examples (and corner cases) of metaprogramming
These screencasts assume you have some basic Ruby knowledge.
Brought to You By
Dave Thomas has been using Ruby since 1999, and has been teaching it around the world since 2002. He’s the author of Programming Ruby, Agile Web Development with Rails, The Pragmatic Programmer, and four other titles. He was one of the authors of The Agile Manifesto. But mostly he’s a Ruby programmer who still loves the language, and loves telling other people how cool it is.