Category Ruby metaprogramming

Ruby Object Model and Metaprogramming Screencasts by Dave Thomas

I bought 6 screencast episodes of Ruby metaprogramming about 3 years ago and only recently have I watched them all. And I must say: it was quite worth it! Just a reminder that it’s never too late 🙂 See below for my notes (which have been approved by Dave Thomas himself). Episode 1 – Objects […]

Unobstrusive metaprogramming, by Sean O’Halpin

I watched Sean O’Halpin 30-minute Ruby Manor presentation (Nov 2008) called “Unobstrusive metaprogramming”. He gave a few tricks about monkeypatching. Here are my highlights. Problems with monkey patching “the Rails way” namespace pollution documentation pollution cognitive pollution Case in point: >> [Kernel, Object, Class, Module].inject({}){|hash, klass| hash[klass] = klass.methods.size; hash} => {Object=>98, Kernel=>159, Module=>99, Class=>99} >> […]

Ruby Internals, by Patrick Farley

I’ve watched the presentation Ruby Internals, from Patrick Farley, and I thought it was really interesting. Here are my own highlights: The building blocks of MRI’s implementation are C structs: RObject, RBasic, RClass. instance methods (behavior) in a Ruby class are stored in a C struct called RClass (and not in RObject) method dispatch will […]

A first shot at metaprogramming in Ruby

If you find this interesting/funny: irb(main):001:0> “my_string”.class=> Stringirb(main):002:0> “my_string”.class.class=> Classirb(main):003:0> String.superclass=> Objectirb(main):004:0> Object.superclass=> nilirb(main):005:0> Class.class=> Classirb(main):006:0> Class.superclass=> Moduleirb(main):007:0> Module.superclass=> Object And this makes you dizzy when you’ll learn that you’re adding a singleton method in the Player class‘ metaclass: class Player @@players = [] class << self def add( player ) @@players << player end […]