Toxic Elephant

Undressed Ruby

Posted by matijs Sun, 14 Oct 2007 10:14:00 GMT

In Ruby, there is really no compile time. There is parse time, and then there is run time. All class and method definitions are done at run time.

  class Foo
    def bar
      puts "Zoo!"
    end
  end

is basically1:

  Foo = Class.new do
    self.define_method(:bar) do
      puts "Zoo!"
    end
  end

See, we just removed the class and def keywords from Ruby. What else? Ah yes, method calls. That’s just sending messages. In fact, you can replace any method call foo.bar with foo.send(:bar), like so2:

  Foo = Class.send(:new) do
    self.send(:define_method, :bar) do
      self.send(:puts, "Zoo!")
    end
  end

How much syntax can you remove like this? How far can Ruby be undressed? And can you come up with a macro system to put the clothes back on?

Some ingredients: Why list macros are cool, RubyToRuby.

1 Actually, it’s not the same, since the define_method way creates a closure, so you can do like this

2 Yes, I know I’m using method call syntax here. But this way I do expose the message-passing view on OO that Ruby has. If you want, you can replace it with send_message(object, method, *args), and assume Ruby defines send_message somewhere.

Posted in  | Tags ,  | no comments | no trackbacks

No comments

No trackbacks

(leave url/email »)

Comment Markup Help Preview comment   

Toxic Elephant is Matijs van Zuijlen's weblog.

Powered

Categories

Archives