string comparison

B

Boris Glawe

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
 
A

ako...

hello,

the below code displays 'equal' on my machine:

s0 = "hello world"
s1 = "hello world"
puts 'equal' if s0 == s1

konstantin
 
C

Christer Nilsson

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
 
B

Boris Glawe

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
 
A

ako...

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
 
W

Wilson Bilkovich

if not obj.text =3D=3D 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 =3D=3D local=
text
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

string comparison 0
string comparison 1
Picture Comparison Code Not Working Properly 1
Python string find() examples 2
Image overlay and comparison code error. 2
Tasks 1
Comparison error 2
comparison of two string 4

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top