Implementing Prototype’s Event.observe in Ruby

I love Prototype. I wanted to implement something akin to Event.observe in some of the gui applications I’ve been playing with. The code’s fugly, but it’s a first pass and I just finished my last Netflix.

Example JavaScript Implementation is pretty:

a = new Test_Object( "First" )
b = new Test_Object( "Second" )

Event.observe(
  a,
  "test_method",
  b.test_method
)

Event.observe(
  b,
  "test_method",
  function(e) { new Test_Object("Third").test_method() }
)

Example Ruby Implementation..not so pretty.

$event = Event.new
a      = Test_Object.new( "First" )
b      = Test_Object.new( "Second" )

$event.observe(
  a,
  "test_method",
  b.method( "test_method" )
)

$event.observe(
  b,
  "test_method",
  Proc.new { Test_Object.new("Third").test_method }
)

a.test_method

Yep, that’s a global. I’m still working on it. Any ideas would be welcome!

Download the Ruby code!