File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Ruby and the fly likes Metaprogramming: essential part of Ruby Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Ruby
Bookmark "Metaprogramming: essential part of Ruby" Watch "Metaprogramming: essential part of Ruby" New topic

Metaprogramming: essential part of Ruby

Michael Sullivan
Ranch Hand

Joined: Dec 26, 2003
Posts: 235
Hello Paolo, thanks for coming by the 'ranch and answering our questions.

I took a look at a review of Metaprogramming Ruby, and one comment stood out - that Metaprogramming was an essential part of Ruby programming. Can you expand on this a bit? I ask because I have reviewed Martin Fowler's information on Thoughtworks use of Ruby/RoR. He notes that metaprogramming can be useful, but only when developers understand it well - and use it appropriately.

I also had a chuckle when I read that you "teach Java", but "love Ruby"!


David Newton

Joined: Sep 29, 2008
Posts: 12617

I also believe it's an essential part of Ruby--if you care about using it well, and to its fullest advantage.
Paolo Perrotta

Joined: Mar 27, 2010
Posts: 27
Hello, Michael! For the sake of explanation, I'll assume that you're a Ruby beginner (sorry if you're not!).

First, be aware that the borders of metaprogramming are somewhat fuzzy in Ruby. As you learn about metaprogramming, you'll change your mind about which techniques are "meta" and which are not. In fact, one of the biggest hurdles I had while writing the book was trying to decide which techniques belonged to "metaprogramming" and which are just plain vanilla Ruby. In the end, there is no hard and fast distinction.

For example, most Ruby programmers are used to Class Macros, and don't think of them as especially "meta". If you don't know what a Class Macro is, think of attr_accessor:

So this is technically an example of metaprogramming: under the hood, attr_accessor wrote two new methods for you, named my_attribute() and my_attribute=(). Even if you never heard about metaprogramming at all, you're supposed to know about attr_accessor if you're an experienced rubyist. If you're advanced, you're also expected to be able to come up with your own Class Macros, like ActiveRecord does when it defines macros such as belongs_to or validates_length_of. To write Class Macros, you probably need to understand how Ruby classes, objects and modules are organized - the so-called "object model".

If you chose to willfully ignore these techniques, you'll probably have trouble understanding the way Ruby works, and you'll have trouble understanding the typical Ruby libraries. This is what I mean when I say that metaprogramming is a core part of Ruby. The language expects that you know how the object model works.

So, how is metaprogramming "bad"? Like all powerful tools, it can be abused. Once you know the ins and outs of the Ruby object model, you have enough of a gun to shoot your own foot. For example, instead of just resorting to a Class Macro, you can open the eigenclass to define a class macro that defines a method by evaluating a string that conditionally composes code that is nearly impossible to understand. (If that's gibberish to you, just replace it with "you can do something that's overly complicated") ;) .

I think that this kind of abuse is what Martin Fowler was talking about. It's a basic trade-off of Ruby: you have a lot of power at your disposal, and you have to draw your own line between using not enough of it, and too much of it. Wherever you put the line, you're supposed to know these techniques before you can take an informed decision.

Paolo Perrotta, Europe
Author of Metaprogramming Ruby
I agree. Here's the link:
subject: Metaprogramming: essential part of Ruby
It's not a secret anymore!