012 RR Metaprogramming in Ruby

by Charles Max Wood on July 24, 2011

Panelists

Discussed in this episode

  • What is metaprogramming
  • AST
  • Ripper
  • method_missing
  • send
  • super
  • defined?(super)
  • alias method chain
  • rdoc
  • rspec

Guidelines

  • Aliasing/Redifining methods (generally wrong)
  • Dynamic Module generation (Generally right)
  • understand Ruby’s method call lookup
  • understand modules (extend and include)
  • Calling super is usually a good idea
  • If you’re going to monkey patch, make sure that the method isn’t already there
  • Make your changes easy to find
  • class_eval def vs define_method
  • Convenience constructors – In a medium sized project, you should not do this more than 3 times.

Picks

8 comments
Jan Wedekind
Jan Wedekind

Full meta-programming would be nice. It would put Ruby on par with Scheme and Smalltalk. One could for example implement optimisers and just-in-time compilers in Ruby without having to modify the VM.

Gregory Brown
Gregory Brown

Pedro, I can see what you're saying. Sorry about that. This is a topic I get all fired up about but at the same time the call was full of people smarter than me that I should have given a lot more space. It wasn't intentional, but I'll keep it in mind for future sessions.

Colin Jack
Colin Jack

"Convenience constructors – In a medium sized project, you should not do this more than 3 times." Was wondering if you could provide a bit more info/ context on this one?

Colin Jack
Colin Jack

Dynamic Module generation - can you provide more info and/or a link? Great show as always, a metaprogramming 2 does indeed sound like it would be useful.

Pedro Nascimento
Pedro Nascimento

Greg should refrain himself from interrupting people that much. It was a little disturbing, honestly. I've got some questions: What is dynamic module generation? I can't find any information on the interwebs. I'll probably have to listen to this episode after I learn about it, I got a little lost without proper context. Are convenience constructors the sort of thing that returns an object of itself? i.e. a 'create_something_awesome' class method initializes an object of the class itself and then applies a bunch of things to it? Please let me know if this is the case. I usually do not take this approach because an extra .new never hurts that much. All and all, awesome podcast as always. I'd love to hear a metaprogramming part 2, it sounded like you guys had a lot more to discuss.

James Edward Gray II
James Edward Gray II

Don't blame Greg too much. We can't see each other when we talk, so it's hard to know who should talk next. Dynamic module generation refers to code that uses something like Module.new() and/or the eval.() methods to build up a construct on the fly, as needed. Here's an example using Class.new() which might give you the idea: http://blog.grayproductions.net/articles/summoning_error_classes_as_needed I believe your description of a convenience constructor is accurate, though I don't think it was me who used the term.

Trackbacks

  1. [...] prominent “Rubyists” discuss the definition of metaprogramming in much greater detail.[1]To start with, metaprogramming is not magical. It will not solve all of your problems. Its usage [...]

Previous post:

Next post: