Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > check if 2 objects are equal

Reply
Thread Tools

check if 2 objects are equal

 
 
junkone@rogers.com
Guest
Posts: n/a
 
      08-01-2006
Hi
I havea a class with this structure. how do i check if 2 objects
instanialted from this class are equal. they could be separate
instantialtion but have exactly the same values?


class SymbolObject

def initialize(symb)
puts symb
@symbol=symb

@name=@exchange=@news=@summary=@sector=@industry=@ category=@otherInfo=@scanType=""
@markedBad=false
end
attr_accessor :symbol,:name ,:exchange, :news, :summary, :sector,
:industry,:category,:markedBad,:scanType,therInf o;
def to_s
return @symbol + @news + @exchange + @name
end


end

 
Reply With Quote
 
 
 
 
N Okia
Guest
Posts: n/a
 
      08-01-2006
If you want to define equivalence for your classes, you really need to
do so yourself.

The easiest way is to make the class Comparable, and define your own
<=> operator. You can be as simple or as complex as you like:

class MyClass
include Comparable

attr_accessor :my_age, :my_first_name, :my_last_name

def initialize(age,first_name,last_name)
@my_age = age.to_i
@my_first_name = first_name
@my_last_name = last_name
end

def <=>(other)
# if the other class being compared is nil, we aren't equivalent
return nil if other.nil?

# Simple case, if both objects have same objectid, they are the same
return 0 if object_id == other.object_id

# Define a heirarchy of tests for this class
if (@my_age == other.my_age)
if (@my_first_name <=> other.my_first_name) == 0
return @my_last_name <=> other.my_last_name
else
return @my_first_name <=> other.my_first_name
end
else
return @my_age <=> other.my_age
end
end

end

 
Reply With Quote
 
 
 
 
Tim Hunter
Guest
Posts: n/a
 
      08-01-2006
N Okia wrote:
> If you want to define equivalence for your classes, you really need to
> do so yourself.
>
> The easiest way is to make the class Comparable, and define your own
> <=> operator. You can be as simple or as complex as you like:
>
> class MyClass
> include Comparable
>
> attr_accessor :my_age, :my_first_name, :my_last_name
>
> def initialize(age,first_name,last_name)
> @my_age = age.to_i
> @my_first_name = first_name
> @my_last_name = last_name
> end
>
> def <=>(other)
> # if the other class being compared is nil, we aren't equivalent
> return nil if other.nil?
>
> # Simple case, if both objects have same objectid, they are the same
> return 0 if object_id == other.object_id
>
> # Define a heirarchy of tests for this class
> if (@my_age == other.my_age)
> if (@my_first_name <=> other.my_first_name) == 0
> return @my_last_name <=> other.my_last_name
> else
> return @my_first_name <=> other.my_first_name
> end
> else
> return @my_age <=> other.my_age
> end
> end
>
> end
>


The simplest way to do this is:

def <=>(other)
return [@my_age, @my_first_name, @my_last_name] <=> [other.age,
other.my_first_name, other.my_last_name]
end
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HashSet keeps all nonidentical equal objects in memory Frederik Java 5 07-20-2011 04:31 PM
Best practise implementation for equal by value objects Slaunger Python 11 08-08-2008 10:25 AM
Internet Sharing: Equal upload speeds but un-equal download speeds =?Utf-8?B?TkpU?= Wireless Networking 3 09-15-2007 06:22 AM
check for divisor equal to zero if when handling floats? Giff C++ 4 05-15-2007 09:32 AM
test whether 2 objects are equal Yves Glodt Python 5 02-01-2006 12:13 AM



Advertisments