views:

449

answers:

2

Will I learn anything in Metaprogramming Ruby that is not covered in any of: Programming Ruby, The Ruby Cookbook, The Ruby Programming Language or The Ruby Way, all of which have at least a chapter's material covering metaprogramming in Ruby?

It also has about a third of the book devoted to Rails, which I'm not particularly interested in reading about. Is it worth buying considering I have the above books?

EDIT: One thing that I am particulary interested in seeing is new applications of metaprogramming to solve problems. It helps fill in the gaps in my knowledge and feeds my imagination in finding different ways to look at problems themselves.

+1  A: 

A book about metaprogramming should be a book about when to not metaprogram; it's one of the most overused concepts these days.

Compare

%w(flush_all version quit).each { |cmd|
  class_eval do
    define_method "#{cmd}" do
      book_it(cmd.to_sym)
      send_to_peer "#{cmd}\r\n"
    end
  end
}

to

def flush_all; send_command(:flush_all) end
def version;   send_command(:version)   end
def quit;      send_command(:quit)      end

def send_command(cmd)
  book_it(cmd)
  send_to_peer "#{cmd}\r\n"
end

(Taken from http://ozmm.org/posts/define_method_and_rdoc.html)

Metaprogramming can make sense, but it can also mess up your code. This probably didn't answer your question, but it's something worth being aware of anyway.

August Lilleaas
Indeed, thus my interest in knowing what uses people put it to, so that those uses can be categorized and evaluated. Without it, you're flying blind, knowing a technique without knowing what it is good for.
Pinochle
+4  A: 

Disclaimer: I'm strongly biased when it comes to Metaprogramming Ruby, because I'm the author of the book.

The books you already mentioned don't leave many metaprogramming stones unturned. However, I think you'll still enjoy Metaprogramming Ruby. Try downloading the book samples from http://www.pragprog.com/titles/ppmetr (especially the first pages of the "Methods" chapter and the "Spell Book"). If you like those, you'll probably like the rest of the book.

The part of the book that focus on Rails is way shorter than one third - just about 40 pages. It just looks at the Rails source for examples of metaprogramming, so you don't really have to be interested in Rails to read it.

Paolo Perrotta
I was hoping that I would shake you out of the bushes! The one thing that I might be looking from from your book is something that is sometimes missing from most discussions of metaprogramming in any language --- talking about what it is good for (using evidence of **specific** problems that are better handled using meta-programming etc...) and specific examples of where it gets one into problems. Usually such discussions are quite hand-wavey or reduced to laundry lists (maintainability: yes or no, etc...) in such a way that they are not particularly useful.
Pinochle
Everybody loves clean-cut advice like: "Do (or do not) use metaprogramming in this specific case". The problem is that most metaprogramming techniques are very context-dependent, or haven't been explored enough yet to draw a stark line. I try to give some advice in the book, but some solutions that look great to me might feel terrible to you, depending on your experience, team size, project size, personal tastes, amount of tests, and so on. YMMV.By all means let me know if you find the book pragmatic enough for you. I'm trying to provide a bit more specific advice in the new Rails chapters.
Paolo Perrotta
Yes, in a sense you're right. I'm just wondering whether we know enough now to move metaprogramming from "art" to "science" and categorize the types of problems that can be fruitfully dealt with using metaprogramming techniques. I think I will take a look at the book, especially since you're using rails as "evidence". I was afraid the Rails section would spend (too much) time "teaching Rails"; it is a good example of the use of metaprogramming though.
Pinochle