From: Dossy Date: 2002-08-25T10:24:08+09:00 Subject: Re: CompareByValue On 2002.08.25, Ryan King wrote: > I put a challenge up at: > http://www.rubygarden.org/ruby?CompareByValue I don't think this is the /best/ solution, but it's one that makes all ten tests pass: module CompareByValue def ==(other) # return true if self.id == other.id return false unless self.type == other.type return false unless self.instance_variables == other.instance_variables self.instance_variables.each { |name| next if self.id == self.instance_eval(name).id next if other.id == other.instance_eval(name).id next if self.id == other.instance_eval(name).id next if other.id == self.instance_eval(name).id return false unless self.instance_eval(name) == other.instance_eval(name) } true end end The commented-out line is not required to make the tests pass (that's why it's commented out) but it's an optimization that might make the comparison go faster. > http://www.rubygarden.org/ruby?CompareByValueTests (which isn't > even a thorough test suite, necessarily). What kind or class of tests might be missing? > I'm curious to see if this list can come up with something that's > elegant and that works. I'd rather see the list come up with a comprehensive list of tests that would really give confidence that whatever code passes the test will work. ;-) -- Dossy -- Dossy Shiobara mail: dossy@panoptic.com Panoptic Computer Network web: http://www.panoptic.com/ "He realized the fastest way to change is to laugh at your own folly -- then you can let go and quickly move on." (p. 70)