From: Jeff Wood Date: 2005-10-08T00:27:27+09:00 Subject: Re: Event Handling ------=_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 wrote: > > James Edward Gray II wrote: > > On Oct 7, 2005, at 8:46 AM, iamscottwalter@gmail.com 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/libdoc/observer/rdoc/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 < end > end > > Sorry, just loud thinking... > > Kind regards > > robert > > > -- "http://ruby-lang.org -- do you ruby?" Jeff Wood ------=_Part_13099_4540671.1128698844549--