From: Dossy Date: 2002-08-29T09:28:40+09:00 Subject: Re: CompareByValue On 2002.08.27, Ryan King wrote: > > I added another test, which makes this implementation fail, too. Terrific! I updated the code that makes the new test pass. > The implementation could be hacked so that this passes. However, > I wrote it as a simple case to represent an arbitrarily recursive > structure. > > The recursion is what makes this problem a "Challenge" rather > than a "Trivial Chunk of Code". =) I don't think my solution to make the test pass is a "hack" that just makes the test pass, but the whole class of tests involving recursion, to pass. http://www.rubygarden.org/ruby?DossysCompareByValue Here's the code for folks to review: module CompareByValue def CompareByValue.compare(a, b) return true if a.equal? b return false unless a.instance_of? b.type return false unless a.instance_variables == b.instance_variables a.instance_variables.each { |name| next if a.equal? a.instance_eval(name) next if a.equal? b.instance_eval(name) return false unless CompareByValue.compare(a.instance_eval(name), b.instance_eval(name)) } true end def ==(other) return CompareByValue.compare(self, other) end end -- 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)