From: Daniel Sheppard Date: 2005-10-21T14:47:04+09:00 Subject: Re: Reflection, observ(er|able) instance vars? Problem with the == method you defined is that hash equality != object equality - especially for your hashing method. This module will provide you with the hash and eql? methods (alias those to other methods as appropriate). The hash function is cached, and reset whenever a var= method is called (make sure you don't go modifying hashed vars directly). This is probably not the only/best way to do this, but it made sense to me: module FastHashEquals def attr_hashing( *arr ) attr_reader *arr hashers = class_eval "@@__hashers ||= []" hashers.concat(arr) arr.each do |var| define_method(var) { instance_variable_get("@#{var}") } define_method("#{var}=") {|obj| instance_variable_set("@#{var}", obj) instance_variable_set(:@__equals_hash, nil) } define_method('eql?') { |other| hash === other.hash && hashers.all? { |x| send(x) == other.send(x) } } define_method('hash') { @__equals_hash ||= hashers.inject(0) { |s,x| s + instance_variable_get("@#{x}").hash } } end end end To use: class HashTest extend FastHashEquals attr_hashing :one, :two, :three end > -----Original Message----- > From: Trans [mailto:transfire@gmail.com] > Sent: Friday, 21 October 2005 9:32 AM > To: ruby-talk ML > Subject: Re: Reflection, observ(er|able) instance vars? > > Use Module, or better yet your own mixin. And just do the > class eval yourself w/o metaid, it's pretty easy > > def ==( other ) > hash == other.hash > end > end ##################################################################################### This email has been scanned by MailMarshal, an email content filter. #####################################################################################