From: Gavin Sinclair Date: 2004-06-18T23:56:41+09:00 Subject: Re: How to make generic #== method? On Saturday, June 19, 2004, 12:28:21 AM, Florian wrote: > Moin! > Joel VanderWerf wrote: >> module ContentEquality >> def hash >> content.hash >> end >> >> def eql?(other) >> content.eql? other.content >> end >> >> def ==(other) >> # self.class == other.class and # optional >> content == other.content >> end >> end > That's cool, I think I'm going to use it in the future! :) >> Another way might be to iterate over a list of instance variables (or >> all of 'em, if that's what you want). > Hm, maybe a default .content could be provided by the module; it could > look like this: > module ContentEquality > def content > instance_variables.map { |name| instance_variable_get(name) } > end > end Because this discards the instance variable names, some pathological cases would give false positives. Better to make #content return a hash instead. Gavin