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:
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.
#####################################################################################
Problem with the == method you defined is that hash equality != object
equality - especially for your hashing method.
Yes, I'd spotted that and was going to take that into account.
Thanks
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:
This "delivers us from eval" but does define_method offer
anything else over the class_eval %{...} approach? Maybe [only :-)]
speed?
Daniel makes some fair points, but there's nothing wrong with using
class_eval here. In fact with eval it's more concise the resulting code
will be faster.