Forum: Ruby testing equality

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1b30afec8cd3efa4c7a10980c10315d8?d=identicon&s=25 Haris Bogdanovic (Guest)
on 2009-03-09 11:35
(Received via mailing list)
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
6e366eb5a71be2bad7f383d42aeb4788?d=identicon&s=25 Justin Collins (Guest)
on 2009-03-09 11:43
(Received via mailing list)
Haris Bogdanovic 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-03-09 12:54
(Received via mailing list)
2009/3/9 Justin Collins <justincollins@ucla.edu>:
> Haris Bogdanovic 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
1b30afec8cd3efa4c7a10980c10315d8?d=identicon&s=25 Haris Bogdanovic (Guest)
on 2009-03-11 21:40
(Received via mailing list)
I got it. One string from comparison is from gets function so it has
'enter'
character added on the end.


"Haris Bogdanovic" <fbogdanovic@xnet.hr> wrote in message
news:gp2r50$usq$1@gregory.bnet.hr...
This topic is locked and can not be replied to.