From: avi@...4.com (Avi Bryant) Date: 2001-11-15T10:38:39+09:00 Subject: [ruby-talk:25234] Re: attr_accessor question Michael Sullivan wrote: > I was wondering if when one has defined "attr_accessor" for their > instance variables if there was any way to determine when one of the > variables changes. We really need a more generic attr framework, so that I can write something like class Foo attr [:read, :write, :initialize, :notify] :x, :y end (and while we're at it, we need to be able to do the same thing for methods and classes...) But for now, how about: def Module.attr_notify(*vars) vars.each do |var| module_eval %{ def #{var}() @#{var} end def #{var}=(_#{var}) @#{var} = _#{var} changed(:#{var}) end } end end This is untested, but it should allow: class Foo attr_notify :x, :y def changed(var) puts "#{var} was changed!" end end