A first shot at metaprogramming in Ruby

If you find this interesting/funny:

irb(main):001:0> "my_string".class
=> String
irb(main):002:0> "my_string".class.class
=> Class
irb(main):003:0> String.superclass
=> Object
irb(main):004:0> Object.superclass
=> nil
irb(main):005:0> Class.class
=> Class
irb(main):006:0> Class.superclass
=> Module
irb(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
end
end

Then you should have a look at this, written by whytheluckystiff.

Advertisements

Got a comment?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: