From: Shawn Anderson Date: 2008-05-08T16:11:35+09:00 Subject: Re: Event handling ------=_Part_36211_23285653.1210230692782 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline This seems like a decent approach; maintaining a list of listeners and notifying them. A few notes: for i in 0...@listeners.length @listeners[i].sound_event('MHOEEEE') end Closures or blocks in ruby are perfect for this: @listeners.each do |listener| listener.sound_event 'MHOEEEE' end to get cool eventing like: @car.when :crashing do jump_out end I recommend looking at Publisher: http://rubyforge.org/projects/atomicobjectrb/ On Wed, May 7, 2008 at 10:42 PM, Aa Aa wrote: > Oke, I've implemented the following observer/listener program: > > class Cow > def initialize > @listeners = Array.new > end > > def add_listener(listener) > raise ArgumentError if !listener.respond_to?(:sound_event) && > !listeners.include?(listener) > @listeners << listener > end > > def remove_listener(listener) > @listeners.delete(listener) > end > > def notify_listeners > for i in 0...@listeners.length > @listeners[i].sound_event('MHOEEEE') > end > end > > def be_happy > notify_listeners > end > end > > class Program > def sound_event(value) > puts value > end > > def start > cow = Cow.new > cow.add_listener(self) > cow.be_happy > end > end > > Program.new.start > > Is this an appropriate way of doing event handling in ruby? > -- > Posted via http://www.ruby-forum.com/. > > ------=_Part_36211_23285653.1210230692782--