From: Joel VanderWerf Date: 2006-08-25T04:04:58+09:00 Subject: Re: Observing changes in object state Max Muermann wrote: > Hi all, > > I was playing around with the observer library and thought I'd > reimplement somthing I had done in Java a while back. It uses the > observer pattern but automatically adds interceptors to every > attribute setter method - once on the first include and then > dynamically as more setter methods are added. If anybody has some > comments or hints on how to implement this in a better way, I'd be > delighted to hear them. Here's a somewhat different approach, but it might be interesting to compare: require 'observable' # see http://raa.ruby-lang.org/project/observable/ class Model extend Observable attr_accessor :id # avoid confusion with Object#id observable :name, :id end # Observer class Watcher def initialize m m.when_name Object do puts "name set to #{m.name.inspect}" end m.when_id Object do puts "id set to #{m.id.inspect}" end # note that each method is treated separately end end # change some stuff m = Model.new Watcher.new m m.id='000' m.name='test' m.id='000' # note this does not trigger the observer since value # doesn't change. Use the "signal" mechanism from the # observable lib for that behavior __END__ Output: name set to nil id set to nil id set to "000" name set to "test" -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407