From: Joel VanderWerf Date: 2004-06-18T10:15:44+09:00 Subject: Re: How to make generic #== method? Zakaria wrote: > Hi, > > Currently I got many simple class which is just a simple record-like. > An example > ----------------------------------- > class LoopTag # :nodoc: > attr_reader :param > attr_reader :contents > > def initialize(param) > @param, @contents = param, [] > end > > def ==(other) > return false unless other.class == self.class > return other.param == @param && other.contents == @contents > end > end > ------------------------------------ > and I got a couple more class like this. > > Is there a way to make a generic #== or some Module that I can include ? One way I've done it is this: 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 Just include the module and define a content method that returns something like and array of objects that are significant for comparison purposes: class LoopTag include ContentEquality def content [@param, @contents] end end Another way might be to iterate over a list of instance variables (or all of 'em, if that's what you want).