From: Hugh Sasse Date: 2005-10-21T19:01:28+09:00 Subject: Re: Reflection, observ(er|able) instance vars? On Fri, 21 Oct 2005, Trans wrote: > Use Module, or better yet your own mixin. And just do the class eval > yourself w/o metaid, it's pretty easy > > module HashAttr > > def attr_hashing( *arr ) > attr_reader *arr > arr.each do |var| > class_eval %{ > def #{var}(val) ; @var ; end > } > class_eval %{ > def #{var}=(val) > @var=val > @thehash = nil #reset > end > } > end > end > > end That's very nice! :-) > > Then do > > class Wombat > extend HashAttr > > attr_hashing :wom, :bat > attr_reader :thehash > > def initialize > @wom = "wom" > @bat = "bat" > @thehash = nil > end > > def hash > puts "hash called" > @thehash ||= (@wom.to_s + @bat.to_s).hash > end > > def to_s > @wom.inspect + @bat.inspect > end > > # compare > def ==( other ) > hash == other.hash > end > end > > HTH, Yes, that's much simpler than the knots I'd got myself into. Thank you. That will help considerably. > T. > Hugh > >