Tim Pease
12/19/2006 6:06:00 PM
On 12/19/06, Tim Pease <tim.pease@gmail.com> wrote:
> On 12/19/06, Jeremy Wells <jwells@servalsystems.co.uk> wrote:
> > Lobosque Lucas wrote:
> > > Is there a way to compare two objects without take its ID in
> > > consideration? For example:
> > >
> > > #<Game_Esper:0xdcc7e0 @str=6, @mdfel = 0, @name = "Ifrit"> ==
> > > #<Game_Esper:0xeda51e @str=6, @mdfel = 0, @name = "Ifrit">
> > >
> > > It'll return false, because the object ID is different. But i want it to
> > > return true, because the arguments (is it the right name?) are all
> > > equal.
> > >
> > > Thanks
> > >
> > You can overwrite the == method:
> >
> > def ==(other_obj)
> > true if self.str == other_obj.str and self.mdfel == other_obj.mdfel
> > and self.name == other_obj.name
> > end
> >
>
A little, little more robust ...
def ==( other )
return false unless self.class === other
self.instance_variables.each do |v|
return false unless self.instance_variable_get(v) ==
other.instance_variable_get(v)
end
true
end
Blessings,
TwP