19 April 2008
Metaprogramming Ruby
On this year’s RuPy I gave a talk about Metaprogramming Ruby. Unfortunately I didn’t have the time to prepare the presentation exactly the way I wanted, nor did the organisators gave me enough time to present what I wanted to present. Anyway, I made some changes to the slides, which are now of some value on they own I believe, so I put them here for everybody to enjoy. Things covered include: Ruby’s object model, differencies between instance_eval/class_eval, hooks, reflection, wrapping methods, DSLs, Higher-Order Messanging, getting the parse tree from Ruby code and vice versa, and so on. There is also a nice resources section, where you will find the gems I found across all the available material about the topics covered, and from which I’ve picked up a lot of really interesting meta-ideas. If you find any errors, please let me know, this stuff is really fuzzy, and I really want to have it covered in a clear and precise way.
Despite the quirk with my own presentation, I really enjoyed RuPy as a participiant and I think it’s a great event. Zed Shaw had a really nice presentation, with some funny bits, but also with many things you can pick up and use weeks after the conference ended. I also liked the talk about PyPy by Carl Friedrich Bolz, even through I’m a Ruby guy - they really have some interesting ideas about compiling/interpreting dynamic languages and they made a hell of the effort to push it forward. I hope I will be able to visit Poznań again the next year and I encourage you to do the same too.
Comments ():
Michał Kwiatkowski, 19 April 2008, 9:39 pm
On the 9th slide (”Ruby’s object model #2″) there seems to be an error, which I’ll allow myself to correct. ;)
It seems that two different concepts got intertwined there.
There’s this concept of class hierarchy - some classes are above others. Those below inherit methods from those above. A class that in a class hierarchy is above another is called its superclass. So, talking about superclasses makes sense only in a context of a class and its position in the class hierarchy.
And there’s a concept of an object being an instance of a class. Class of an object can also be called its “type”. In single-dispatch systems class holds methods, so it defines a behaviour of its instances.
Getting back to the slide, “a string” cannot have a superclass, because it’s not a class. But its class is truly a String. In other words, “a string” is an instance of a class String.
Now, String is a class, so it’s safe to talk about its superclass = a class that stands higher in the class hierarchy. Superclass of String is not Class, as the slide suggests, but it’s Object. Here again, you probably meant that String is of type (or of class, as we may say) Class. In other words, you looked at String as an object and asked for its class. Class of String is Class. :)
Then again, Class is of type Class, which wouldn’t be that interesting and would cut the slide in a half. ;) So, now we look at classes in context of class hierarchy. By asking what is higher (”super”) you go from Class to Module, and from Module to Object. That part is OK.
Apart for this error, I really enjoyed reading your slides. I think you made a good approach of going to the source and referring to the Ruby interpreter’s source code. It also helped me understand some of the Ruby quirks in the past.
And it’s the first time I hear about Higher Order Messaging, very interesting, thanks! Keep up the good work and please blog more frequently. :) BTW, how’s your 10 languages/10 projects idea going?
stiff, 19 April 2008, 10:01 pm
You’re absolutely right, thanks! I fixed it by changing two “superclass” labels to “class” - after you’ve corrected me I’m not really sure if mixing the two kinds of relationship in one picture is the best idea, but from the other side, I still think it helps with forming a simple mental model of what’s going on in Ruby under the hood when doing OO. Maybe I will do some more serious changes to the picture tomorrow…
As for the 10 languages/10 projects idea, I was really afraid someone will ask about it ;) I didn’t have any time to even start working on it, which is really a shame. I might have a chance to repair it soon through, I hope, so maybe I’ll manage to write some first posts from the series :)
Stuart, 22 April 2008, 6:56 am
Really interesting presentation. Thanks for posting this
Sam Aaron, 22 April 2008, 11:30 pm
Hey Jarosław,
really nice slides with great content - well done :-) I’m really enjoying reading them. Was the presentation recorded at all?
I also hope you don’t mind me pointing out a really tiny error on the slide entitled “The tricks - evaluating things #2″. You declare the following:
(class
Sam Aaron, 22 April 2008, 11:33 pm
Hmm, not sure what ate the rest of my previous post, but essentially I was just saying that you should either not use the metaclass or call class_eval in the third stanza. As it stands, the code AClass.class_meth doesn’t currently work.
Anyway, keep up the excellent work, and I hope to be reading more interesting stuff from you soon!
Sam Aaron, 22 April 2008, 11:54 pm
Oh, and a similar error is in the next slide (The tricks - defining things #2). Again you call instance eval on the metaclass, which essentially creates a metaclass of the metaclass of the class ;-)
Your use of the variable ‘description’ in the defined method is only evaluated when it is called, by which time, the value that you passed to it in the class declaration to the method enhance me has long gone. This therefore results in a NameError.
Sorry to point another couple of little errors out. However, once these little niggles are polished out, these slides are going to shine :-)
stiff, 23 April 2008, 7:52 am
You’re right - that’s because I switched the examples from using define_method to def, without actually checking if it works, which hit back at me - fixed :)
Their may be some video from the presentation available in a few days, but I’m not sure if it will be of any use…
thorben, 31 July 2008, 4:33 pm
you still alive?
stiff, 31 July 2008, 4:59 pm
Sure, just busy ;)
Stifflog - What is wrong with Ruby, 16 December 2009, 5:14 pm
[...] can see my metaprogramming presentation for advanced examples of various Ruby oddities in this area, one simple example is the situation [...]