small medium large xlarge

Metaprogramming Ruby 2: Program Like the Ruby Pros


Cover image for Metaprogramming Ruby 2

Metaprogramming Ruby 2

Program Like the Ruby Pros


Write powerful Ruby code that is easy to maintain and change. With metaprogramming, you can produce elegant, clean, and beautiful programs. Once the domain of expert Rubyists, metaprogramming is now accessible to programmers of all levels. This thoroughly revised and updated second edition of the bestselling Metaprogramming Ruby explains metaprogramming in a down-to-earth style and arms you with a practical toolbox that will help you write your best Ruby code ever.

Customer Reviews

This is the one book about Ruby that makes you go “So that’s how it works” over
and over again, as concepts such as the object model, DSLs, and blocks fall into
place with that satisfying “Click!” sound. It’s a great guide to what happens under
the hood of a language that seems to involve a kind of magic deep inside. I highly
recommend it.

- Peter Bakhirev

Lead software engineer, Gilt City

The previous edition of Metaprogramming Ruby changed my life and my code, and
helped me get my first programming job. You would think there would be no way
to improve on a book that good, but Paolo Perrotta has done it. Learn to unlock
the hidden potential of this beautiful language, and fall in love with Ruby again.

- Richard Schneeman

Programmer, Heroku

For gem authors and application developers alike, this book lays down the foundation
everyone needs to harness the full power of Ruby. Paolo describes
metaprogramming in a fun and approachable way for all skill levels. The knowledge
garnered from reading this book will help you write cleaner code and work more
effectively with legacy codebases.

- Paul Elliott

Rocketeer, Hashrocket

If you want to follow the path of Ruby metaprogramming mastery, then this book
is the best companion you can think of, no matter what your level is. I had
struggled with Ruby metaprogramming for years until I read this book; now it all
makes sense.

- Fabien Catteau

Software developer, Tech-Angels

This is a book that everyone who wants to have a deeper understanding of the
inner workings of Ruby and Ruby on Rails should read. The “spells” described in
this book are invaluable tools to understand and use Ruby to its full extent. This
is not only about metaprogramming, but also about taking your Ruby programming
to a different level.

- Kosmas Chatzimichalis

Software engineer

I’m a huge Python fan, so I was supposed to disregard Ruby. Paolo made me appreciate
it. Not only is Metaprogramming Ruby the book that allowed me to wrap
my head around this esoteric and fascinating topic, but it also made me rethink
the way I write code in other languages.

- Arialdo Martini


This book uncovers all the ins and outs of the art of metaprogramming in Ruby,
with a no-nonsense approach and an irony that transpires from vibrant prose,
never boring, without compromising any of its insightfulness. Metaprogramming Ruby is one of those books that any serious Rubyist (and even the pros) will want
to revisit from time to time.

- Piergiuliano Bossi

Principal engineer lead, Points

Metaprogramming Ruby has been a hugely influential book for me, especially
during a time when I wanted to learn about the inner workings of Ruby. Paolo
“Nusco” Perrotta made what is normally a complex topic fun, enjoyable, and very

- Josh Kalderimis

CEO, Travis CI

See All Reviews

Choose Your Format(s)

  • $25.00 In Stock
  • Ebooks are DRM free.

  • Ebook delivery options.

About this Title

Pages: 276
Published: 2014-08-11
Release: P3.1 (2020-04-23)
ISBN: 978-1-94122-212-6

Dig under the surface and explore Ruby’s most advanced feature: a collection of techniques and tricks known as metaprogramming. In this book, you’ll learn metaprogramming as an essential component of Ruby and discover the deep, non-obvious details of the language. Once you understand the tenets of Ruby, including the object model, scopes, and singleton classes, you’re on your way to applying metaprogramming both in your daily work assignments and in your fun, after-hours projects.

Metaprogramming Ruby, Second Edition makes mastering the language enjoyable. The book is packed with:

  • Pragmatic examples of metaprogramming in action, many of which come straight from real-life gems such as Rails.
  • Programming challenges that let you experiment and play with some of the most out-there metaprogramming concepts.
  • Metaprogramming “spells”—33 practical recipes and idioms that you can study and apply right now, to write code that is sure to impress.

This completely revised new edition covers the new features in Ruby 2.0 and 2.1, and contains code from the latest Ruby libraries, including Rails 4. Most examples are new, “from the wild,” with more recent libraries. And the book reflects current ideas of when and how much metaprogramming you should use.

Whether you’re a Ruby apprentice on the path to mastering the language or a Ruby wiz in search of new tips, this book is for you.

What You Need

Ruby 2.x, Ruby 1.9, or a recent version of JRuby.

User Level

Contents & Extracts


  • Metaprogramming Ruby
    • The M Word
      • Ghost Towns and Marketplaces
      • The Story of Bob, Metaprogrammer
      • Metaprogramming and Ruby
    • Monday: The Object Model
      • Open Classes
      • Inside the Object Model
      • Quiz: Missing Lines
      • What Happens When You Call a Method?
      • Quiz: Tangle of Modules
      • Wrap-Up
    • Tuesday: Methods excerpt
      • A Duplication Problem
      • Dynamic Methods
      • method_missing
      • Quiz: Bug Hunt
      • Blank Slates
      • Wrap-Up
    • Wednesday: Blocks
      • The Day of the Blocks
      • Quiz: Ruby#
      • Blocks Are Closures excerpt
      • instance_eval()
      • Callable Objects
      • Writing a Domain-Specific Language
      • Quiz: A Better DSL
      • Wrap-Up
    • Thursday: Class Definitions
      • Class Definitions Demystified
      • Quiz: Class Taboo
      • Singleton Methods
      • Singleton Classes
      • Quiz: Module Trouble
      • Method Wrappers
      • Quiz: Broken Math
      • Wrap-Up
    • Friday: Code That Writes Code
      • Coding Your Way to the Weekend
      • Kernel#eval
      • Quiz: Checked Attributes (Step 1)
      • Quiz: Checked Attributes (Step 2)
      • Quiz: Checked Attributes (Step 3)
      • Quiz: Checked Attributes (Step 4)
      • Hook Methods
      • Quiz: Checked Attributes (Step 5)
      • Wrap-Up
    • Epilogue
  • Metaprogramming in Rails
    • Preparing for a Rails Tour
      • Ruby on Rails
      • Installing Rails
      • The Rails Source Code
    • The Design of ActiveRecord
      • A Short ActiveRecord Example
      • How ActiveRecord Is Put Together
      • A Lesson Learned
    • ActiveSupport’s Concern Module excerpt
      • Rails Before Concern
      • ActiveSupport::Concern
      • A Lesson Learned
    • The Rise and Fall of alias_method_chain
      • The Rise of alias_method_chain
      • The Fall of alias_method_chain
      • A Lesson Learned
    • The Evolution of Attribute Methods
      • Attribute Methods in Action
      • A History of Complexity
      • A Lesson Learned
    • One Final Lesson
      • Metaprogramming Is Just Programming
  • Appendixes
    • Common Idioms
      • Mimic Methods
      • Nil Guards
      • Self Yield
      • Symbol#to_proc()
    • Domain-Specific Languages
      • The Case for Domain-Specific Languages
      • Internal and External DSLs
      • DSLs and Metaprogramming
    • Spell Book
      • The Spells


Paolo Perrotta has fifteen years of experience as a developer, ranging from embedded to enterprise software, computer games, and web applications. Paolo lives a nomadic life, mentoring agile teams throughout Europe. He has a base camp in Bologna, Italy. He loves Ruby.