From: dblack@... Date: 2006-08-28T06:06:31+09:00 Subject: Re: Detecting when an object's instance variables are modified Hi -- On Mon, 28 Aug 2006, David Vallner wrote: > Paul Murton wrote: >> I have a whole load of objects which I would like to periodically dump as >> YAML. However, in order to save on resources, I would prefer to only dump >> those whose instance variables have changed since the last dump. >> >> Is it possible to somehow 'mark' an object when the value any of its >> instance variables is changed? >> > > For the more "magical" way, you could play around with set_trace_func to > monitor setters. Would probably be horribly, terribly slow. > > It's probably best to have your objects trace changes by design. Modify the > Object class to have a #dirty? and #clean! method, and alias / redefine the > #attr_writer method to generate methods that will set the dirty flag when > used? I was thinking of something similar, but using Observable -- but the problem is that it would only work for instance variable changes that go through attr_writer. David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log http://www.manning.com/black => book, Ruby for Rails http://www.rubycentral.org => Ruby Central, Inc.