# Testing equality

**URL:** <https://www.ruby-forum.com/t/testing-equality/162376>\
**Category:** Ruby\
**Created:** [March 9, 2009, 11:35am UTC](https://www.ruby-forum.com/t/testing-equality/162376 "2009-03-09T11:35:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [March 9, 2009, 11:35am UTC](https://www.ruby-forum.com/t/testing-equality/162376/1 "2009-03-09T11:35:35Z")

</div>

Hi.

When I try to test equality of two strings, for example:

if object1.string==object2.string then puts object1.string end

I get nothing printed even though two strings are equal.

Why ?

Thanks

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [March 9, 2009, 11:43am UTC](https://www.ruby-forum.com/t/testing-equality/162376/2 "2009-03-09T11:43:47Z")

</div>

Haris B. wrote:

> Thanks

The best way is to try the code using irb and post the results if you  
are still stuck.  
Most likely, though, the objects you are comparing not really equal. Try  
printing out the values before you compare them.

-Justin

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [March 9, 2009, 12:54pm UTC](https://www.ruby-forum.com/t/testing-equality/162376/3 "2009-03-09T12:54:13Z")

</div>

2009/3/9 Justin C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Haris B. wrote:
> 
> > When I try to test equality of two strings, for example:
> > 
> > if object1.string==object2.string then puts object1.string end
> > 
> > I get nothing printed even though two strings are equal.
> > 
> > Why ?

Apparently those objects are not equal.

> The best way is to try the code using irb and post the results if you are  
> still stuck.  
> Most likely, though, the objects you are comparing not really equal. Try  
> printing out the values before you compare them.

And when doing that p is preferred to puts because it will reveal any  
additional whitespace that you would not see otherwise.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [March 11, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/testing-equality/162376/4 "2009-03-11T21:40:43Z")

</div>

I got it. One string from comparison is from gets function so it has  
‘enter’  
character added on the end.

“Haris B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:gp2r50$usq$removed\_email\_address@domain.invalid…
