From: Joel VanderWerf Date: 2005-10-10T06:44:08+09:00 Subject: Re: Event Handling iamscottwalter@gmail.com wrote: > Hi, > > I am coming from the Java world and was wondering if there ins anything > in Ruby similar to Java's event processing? For example I have a > particular Object that can fire off an event to all objects that listen > for that event. A more real world example would be that I have a > delete() method that was called and I want to let all interested > parties know that the delete() method was called. > > In the Java space I would setup an event listener. When the delete() > method would be called I wouold send an event to all my registered > listeners. > > I hope I was clear enough. > > > Thanks in advance, Scott. > Take look at observable (http://raa.ruby-lang.org/project/observable). The main use is declaring obervable attributes that other objects can "hook" code into, to be notified of changes in state. But there is also a "signal" feature for transient events with multiple observers. Here's an example from that project's examples dir: require 'observable' include Observable include Observable::Match module OpenFileExample class FilePicker signal :file_chosen def run self.file_chosen = "foo" end end class FileOpener def initialize file_picker file_picker.when_file_chosen do |file| puts "FileOpener: opening file #{file}." end end end picker = FilePicker.new opener = FileOpener.new(picker) picker.run p picker.file_chosen # signal has passed by now # Output: # FileOpener: opening file foo. # nil end -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407