What is the equivalent of .NET events in Ruby?

Posted by Gishu on Stack Overflow See other posts from Stack Overflow or by Gishu
Published on 2008-10-29T08:07:17Z Indexed on 2010/04/29 8:07 UTC
Read the original article Hit count: 391

The problem is very simple. An object needs to notify some events that might be of interest to observers.

When I sat to validate a design that I cooked up now in Ruby just to validate it.. I find myself stumped as to how to implement the object events. In .Net this would be a one-liner.. .Net also does handler method signature verification,etc. e.g.

// Object with events
public delegate void HandlerSignature(int a);
public event HandlerSignature MyEvent;
public event HandlerSignature AnotherCriticalEvent;

// Client
MyObject.MyEvent += new HandlerSignature(MyHandlerMethod); // MyHandlerMethod has same signature as delegate

Is there an EventDispatcher module or something that I am missing that I can strap on to a Ruby class ? Hoping for an answer that plays along with Ruby's principle of least surprise. An event would be the name of the event plus a queue of [observer, methodName] objects that need to be invoked when the event takes place.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about events