From: mrp@... Date: 2002-03-14T18:45:59+09:00 Subject: Re: how do i: add_observer( { small bit of code } ) Yukihiro Matsumoto wrote: > In message "how do i: add_observer( { small bit of code } )" > on 02/03/13, mrp@cre.canon.co.uk writes: > |observing an object, i want to run a small bit of code when update is > |called. i currently have a small class Wrapper, and set things up like: > | > | x.add_observer( Wrapper.new() { my code }) > | > |where the block parameter is stored in the class instance, and called in > |Wrapper.update(). > | > |is this the rubyest way of doing this? > How about using Proc? > x.add_observer(lambda{ my code }) > Then your observer can be called by "call". more context required from me! i'm using the observer mixin. putting update() methods in observers is fine for 'normal' code, but when writing unit tests etc. i want to use small bits of code like: x.add_observer({ count += 1 }) up until now i've used a small Wrapper class (brief desc above). i'm now wondering if there's a better way of doing this. any ideas? thanks, martin.