Ruby Event Handlers

After programming in Silverlight / .NET for the past few months, I’ve come to appreciate the beauty of the language’s built-in support for Events.  However, this also reminded me about how useless I’ve found Ruby’s Observable mixin to be.  So, I decided to write a Gem that would add support for .NET-style events to Ruby.

Installing the Gem

Just open up a terminal window and run the following command:

gem install -r unobservable

Using the Gem

Here’s a short program that demonstrates the Gem’s usage:

require 'unobservable'

class Button
  include Unobservable::Support

  attr_event :clicked

  def click(x, y)
    raise_event(:clicked, x, y)
  end
end

button  = Button.new
button.clicked.register {|x, y| puts "You just clicked: #{x} #{y}"}

button.click(2, 3)

Where can I find more information?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>