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… read them below or add one }

Ryan Sobol July 25, 2011 at 1:07 am

Have you tried Screenr Business? http://business.screenr.com/pricing/

Reply

Ryan Sobol July 25, 2011 at 1:09 am

Nevermind, I forgot that Screenr is a screencasting tool and not a screen sharing tool!

Reply

Pedro Nascimento July 25, 2011 at 11:02 pm

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.

Reply

James Edward Gray II August 16, 2011 at 1:48 pm

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.

Reply

Colin Jack July 27, 2011 at 9:40 pm

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.

Reply

Colin Jack July 27, 2011 at 9:43 pm

“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?

Reply

Gregory Brown July 29, 2011 at 8:40 pm

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.

Reply

Jan Wedekind August 2, 2011 at 9:27 am

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.

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: