classical geek

QCon 2: Ruby Meta-programming

I spent most of the day at the Ajax track, but snuck off upstairs to join the Ruby crowd for Dave Thomas’ talk on Ruby meta-programming. Partly, it was the draw of a big name, but mostly just for the fun of guddling in low-level language stuff. I’d been playing around with closures in JavaScript in my own talk a couple of hours earlier, and wanted to see how somebody else approached that sort of thing.

Dave spent most of the talk whipping up real-time demos in an interactive shell, which beats my Powerpoint hands down. After flashing up a simple ActiveRecord class at the outset, there wasn’t much reference to Rails until the very end, when the Fred and Dave examples came full circle to demonstrate how has_many is added to ActiveRecord classes.

One of the unifying principles in Ruby is that everything is an object, even class definitions. Ruby supports metaclasses (kind of invisible little classes that allow custom members to be added to individual instances of objects. Because classes are objects, a class can be customised via its metaclass too, resulting in something that looks like a static method (to drop back into Java-speak).

Most online resources point to Why’s intriguing Seeing Metaclasses Clearly, or you can visit the dungeons of Dwemthy’s Array if you’re feeling foolhardy and brave (but don’t say I didn’t warn you!). A more sober explanation of metaclasses can be found at mobcode.

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

Powered by WordPress, Supported by SaveOnRefinance.com