Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

Metaprogramming: essential part of Ruby

Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"!


Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also believe it's an essential part of Ruby--if you care about using it well, and to its fullest advantage.
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic