# Check if 2 objects are equal

**URL:** <https://www.ruby-forum.com/t/check-if-2-objects-are-equal/100793>\
**Category:** Ruby\
**Created:** [August 1, 2006, 4:16am UTC](https://www.ruby-forum.com/t/check-if-2-objects-are-equal/100793 "2006-08-01T04:16:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 1, 2006, 4:16am UTC](https://www.ruby-forum.com/t/check-if-2-objects-are-equal/100793/1 "2006-08-01T04:16:13Z")

</div>

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,:otherInfo;  
def to\_s  
return @symbol + @news + @exchange + @name  
end

end

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 1, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/check-if-2-objects-are-equal/100793/2 "2006-08-01T14:32:16Z")

</div>

N Okia wrote:

> # Simple case, if both objects have same objectid, they are the same
> 
> ```
> 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 1, 2006, 5:22am UTC](https://www.ruby-forum.com/t/check-if-2-objects-are-equal/100793/3 "2006-08-01T05:22:23Z")

</div>

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
