Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Event Handling

0 views
Skip to first unread message

iamscot...@gmail.com

unread,
Oct 7, 2005, 9:41:42 AM10/7/05
to
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.

Austin Ziegler

unread,
Oct 7, 2005, 9:51:14 AM10/7/05
to

James Edward Gray II

unread,
Oct 7, 2005, 9:55:05 AM10/7/05
to

On Oct 7, 2005, at 8:46 AM, iamscot...@gmail.com wrote:

> Hi,

Hello and welcome.

I think you are looking for observer, in Ruby's standard library:

http://www.ruby-doc.org/stdlib/libdoc/observer/rdoc/index.html

Hope that helps.

James Edward Gray II

Robert Klemme

unread,
Oct 7, 2005, 10:44:55 AM10/7/05
to

This started in interesting thought with me: a block would come in handy
as listener as well. In fact "adapter" might be a better name and what we
get then seems to be quite similar to the C# approach.

class SomeObserver
def register(observable)
observable.add_observer do |*a|
puts "changed" if DEBUG
@model_change = true
# ...
end
end
end

Disadvantages I see so far

- removal of adapters is impossible

- a lot unnecessary adapter objects might be created

Alternative approach:

class SomeObserver
def register(observable)
observable.add_observer adapter(observable) do |*a|
puts "changed" if DEBUG
@model_change = true
# ...
end
end

def adapter(obj,&b)
@adapters[obj] ||= ( b; class <<b; alias :update :call; end; b )
end
end

Sorry, just loud thinking...

Kind regards

robert

Jeff Wood

unread,
Oct 7, 2005, 11:27:27 AM10/7/05
to
You could always make your adapter collection a hash instead of an array,
then you would have a name to be able to replace/delete by.
j.


--
"http://ruby-lang.org -- do you ruby?"

Jeff Wood

Phrogz

unread,
Oct 7, 2005, 2:26:40 PM10/7/05
to

Joel VanderWerf

unread,
Oct 9, 2005, 5:44:08 PM10/9/05
to

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


0 new messages