T
Tim Pease
Working with an Observable object, I wanted to be able to add a Proc
as an observer. Observable requires all observers to implement an
update method. When the Observable state changes, this is the method
that will be called to notify observers of the state change.
My generic solution was to create a module that adds an update method
for Proc objects.
module ProcAsObserver
def update( *args )
self.call *args
end
end
To use this module ...
method = lambda {|*args| args.each {|x| puts x}}
method.extend ProcAsObserver
my_observable_object.add_observer(method)
That's it! Just a handy little tip for anyone working with Observable objects.
Blessings,
TwP
as an observer. Observable requires all observers to implement an
update method. When the Observable state changes, this is the method
that will be called to notify observers of the state change.
My generic solution was to create a module that adds an update method
for Proc objects.
module ProcAsObserver
def update( *args )
self.call *args
end
end
To use this module ...
method = lambda {|*args| args.each {|x| puts x}}
method.extend ProcAsObserver
my_observable_object.add_observer(method)
That's it! Just a handy little tip for anyone working with Observable objects.
Blessings,
TwP