[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

compare two objects without take its ID in consideration

Lobosque Lucas

12/19/2006 12:42:00 PM

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

--
Posted via http://www.ruby-....

13 Answers

Jeremy Wells

12/19/2006 12:56:00 PM

0

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

dblack

12/19/2006 1:16:00 PM

0

Mark Dodwell

12/19/2006 1:37:00 PM

0

It's not particularly nice but you could just do:

re = /:[^\s]*/
obj1.inspect.gsub(re, '') == obj2.inspect.gsub(re, '')


--
Posted via http://www.ruby-....

Mark Dodwell

12/19/2006 1:38:00 PM

0


> obj1.inspect.gsub(re, '') == obj2.inspect.gsub(re, '')

Sorry should be 'sub', not 'gsub':

obj1.inspect.sub(re, '') == obj2.inspect.sub(re, '')


--
Posted via http://www.ruby-....

Lobosque Lucas

12/19/2006 1:40:00 PM

0

Can you explain me what is happening in that code? thanks

--
Posted via http://www.ruby-....

Mark Dodwell

12/19/2006 1:43:00 PM

0

This just gets the representation of both of the objects as strings (the
'inspect') part. Then it strips out the Object ID (the 'sub') part using
a regular expression. It's a bit ugly though.

It would be better to add an attribute to your object which acts as a
identifier for that object (in the real world), then you can simply
compare that to check two objects are the same. For example:

obj1.my_id == obj2.my_id

Hope that makes sense,

Mark


--
Posted via http://www.ruby-....

Tim Pease

12/19/2006 6:05:00 PM

0

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 more robust ...

def ==( other )
return false unless Game_Esper === 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

Tim Pease

12/19/2006 6:06:00 PM

0

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

Ara.T.Howard

12/19/2006 6:34:00 PM

0

Ilan Berci

12/19/2006 7:09:00 PM

0

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

class Game_Esper
def ==(other)
@str==other.str && @mdfel==other.mdfel && @name==other.name
end
end

If you have more state than this, I would suggest a looping construct
instead..

hope this helps

ilan

--
Posted via http://www.ruby-....