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?
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
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