Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Event Handling

Reply
Thread Tools

Event Handling

 
 
iamscottwalter@gmail.com
Guest
Posts: n/a
 
      10-07-2005
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.

 
Reply With Quote
 
 
 
 
Austin Ziegler
Guest
Posts: n/a
 
      10-07-2005
On 10/7/05, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> 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.


http://phrogz.net/ProgrammingRuby/li....html#observer

-austin
--
Austin Ziegler * (E-Mail Removed)
* Alternate: (E-Mail Removed)


 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      10-07-2005

On Oct 7, 2005, at 8:46 AM, (E-Mail Removed) wrote:

> Hi,


Hello and welcome.

> 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 think you are looking for observer, in Ruby's standard library:

http://www.ruby-doc.org/stdlib/libdo...doc/index.html

Hope that helps.

James Edward Gray II



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-07-2005
James Edward Gray II wrote:
> On Oct 7, 2005, at 8:46 AM, (E-Mail Removed) wrote:
>
>> Hi,

>
> Hello and welcome.
>
>> 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 think you are looking for observer, in Ruby's standard library:
>
> http://www.ruby-doc.org/stdlib/libdo...doc/index.html


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

 
Reply With Quote
 
Jeff Wood
Guest
Posts: n/a
 
      10-07-2005
------=_Part_13099_4540671.1128698844549
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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.

On 10/7/05, Robert Klemme <(E-Mail Removed)> wrote:
>
> James Edward Gray II wrote:
> > On Oct 7, 2005, at 8:46 AM, (E-Mail Removed) wrote:
> >
> >> Hi,

> >
> > Hello and welcome.
> >
> >> 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 think you are looking for observer, in Ruby's standard library:
> >
> > http://www.ruby-doc.org/stdlib/libdo...doc/index.html

>
> 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 =3D 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 =3D true
> # ...
> end
> end
>
> def adapter(obj,&b)
> @adapters[obj] ||=3D ( b; class <<b; alias :update :call; end; b )
> end
> end
>
> Sorry, just loud thinking...
>
> Kind regards
>
> robert
>
>
>



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

Jeff Wood

------=_Part_13099_4540671.1128698844549--


 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      10-07-2005
I wrote my own really simple event registration system:
http://phrogz.net/RubyLibs/rdoc/clas...entTarget.html
http://phrogz.net/RubyLibs/EventTarget.rb

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      10-09-2005
(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
python list handling and Lisp list handling Mark Tarver Python 22 04-26-2009 09:36 PM
Is faster handling hexadecimal values than handling chars? IƱaki Baz Castillo Ruby 1 04-15-2008 09:04 AM
ASP.NET Event handling. Makarand ASP .Net 0 07-24-2003 01:46 PM
Pager and Event Handling sunil ASP .Net 2 07-18-2003 09:08 PM



Advertisments