# String comparison

**URL:** <https://www.ruby-forum.com/t/string-comparison/65025>\
**Category:** Ruby\
**Created:** [December 15, 2005, 12:39am UTC](https://www.ruby-forum.com/t/string-comparison/65025 "2005-12-15T00:39:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/87869e/32.png) [@Boris\_G](https://www.ruby-forum.com/u/Boris_G)\
**Post date:** [December 15, 2005, 12:39am UTC](https://www.ruby-forum.com/t/string-comparison/65025/1 "2005-12-15T00:39:16Z")

</div>

Hi,

I have a class containing a string as an instance variable and I have a  
local  
variable containing a string.

I compare these two strings with ‘==’ and with the .eql? method:

###################################

obj.text = “hello world”  
localtext = “hello world”

if obj.text == localtext  
then  
# do something  
end

###################################

The condition never became true in my program. Thus I added some debug  
output.  
This is the the debugging code:

if not obj.text == localtext  
then  
print “”#{obj.text}" is not equal “localtext”\n"  
end

This is, what it gave me:

“hello world” is not equal “hello world”

It’s not worth trying the example above, since it’s not the actual code.  
The  
actual code in my project is much more complex, that’s why I used this  
example  
to discribe my problem.

Fact is that when the string comparison does not succeed I get an output  
like

“hello world” is not equal "hello “world”

Now it’s your turn… What could cause this behaviour?

thanks and greets

Boris

---

<div class="post-metadata">

**Author:** ![Boris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/87869e/32.png) [@Boris\_G](https://www.ruby-forum.com/u/Boris_G)\
**Post date:** [December 15, 2005, 12:48am UTC](https://www.ruby-forum.com/t/string-comparison/65025/2 "2005-12-15T00:48:20Z")

</div>

hello,

the below code displays ‘equal’ on my machine:

s0 = “hello world”  
s1 = “hello world”  
puts ‘equal’ if s0 == s1

konstantin

---

<div class="post-metadata">

**Author:** ![Boris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/87869e/32.png) [@Boris\_G](https://www.ruby-forum.com/u/Boris_G)\
**Post date:** [December 15, 2005, 1:30am UTC](https://www.ruby-forum.com/t/string-comparison/65025/3 "2005-12-15T01:30:28Z")

</div>

> Isolate the problem, and wrap it in a test/unit. Then I can take a  
> closer look.

The problem was a whitespace, which I did not see. I actually wrapped  
the output  
in apostrophs in order to see whitespaces. Since I am programming with  
cgi, I  
see the output in the browser window, which prints whitespaces very  
small.

Anyway thanks for the help!!

greets Boris

---

<div class="post-metadata">

**Author:** ![Boris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/87869e/32.png) [@Boris\_G](https://www.ruby-forum.com/u/Boris_G)\
**Post date:** [December 15, 2005, 12:55am UTC](https://www.ruby-forum.com/t/string-comparison/65025/4 "2005-12-15T00:55:37Z")

</div>

You have real diff here:

> “hello world” is not equal "hello “world”

I tried this code.  
Behaves as expected, prints equal.

class Obj  
attr\_accessor :text  
end

def test  
obj=Obj.new  
obj.text=“world”  
localtext=“world”  
if obj.text == localtext then  
print “equal\n”  
else  
print “not equal\n”  
end  
end

test

Isolate the problem, and wrap it in a test/unit. Then I can take a  
closer look.

Christer

---

<div class="post-metadata">

**Author:** ![Boris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/87869e/32.png) [@Boris\_G](https://www.ruby-forum.com/u/Boris_G)\
**Post date:** [December 15, 2005, 5:22am UTC](https://www.ruby-forum.com/t/string-comparison/65025/5 "2005-12-15T05:22:15Z")

</div>

On 12/14/05, Boris G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if not obj.text == localtext  
> then  
> print “"#{obj.text}" is not equal "localtext"\n”  
> end
> 
> This is, what it gave me:
> 
> “hello world” is not equal “hello world”

Just to remove any precedence weirdness, does this behave differently  
from your failing code?  
puts %Q[#{obj.text} differs from #{localtext}] unless obj.text ==  
localtext

---

<div class="post-metadata">

**Author:** ![Boris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/87869e/32.png) [@Boris\_G](https://www.ruby-forum.com/u/Boris_G)\
**Post date:** [December 15, 2005, 2:21am UTC](https://www.ruby-forum.com/t/string-comparison/65025/6 "2005-12-15T02:21:44Z")

</div>

you can run your cgi script from a command line thus: ‘ruby script.cgi’  
just set REQUEST\_URI environment variable to set the url.

konstantin

---

<div class="post-metadata">

**Author:** ![Boris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/87869e/32.png) [@Boris\_G](https://www.ruby-forum.com/u/Boris_G)\
**Post date:** [December 15, 2005, 9:17pm UTC](https://www.ruby-forum.com/t/string-comparison/65025/7 "2005-12-15T21:17:03Z")

</div>

You might check to see that obj.text.class is the same as  
obj.text.class.  
Also check the sizes of both. I’ve been caught by \n which doesn’t show  
up.
