String Comparison Confusion

J

Joe Johnson

Hi, can someone please explain why the line
puts "YES" if x == 0 returns nil? Does ruby
convert x to a fixnum or convert 0 to a string?
Either case, shouldn't it return TRUE, since the
== operator works for both fixnum and string?
TIA!

irb(main):001:0> x="0"
=> "0"
irb(main):002:0> puts "YES" if x == "0"
YES
=> nil
irb(main):003:0> puts "YES" if x == 0 # CONFUSED LINE
=> nil
 
J

Joe Johnson

Lyle said:
If you make the assignment:

x = "0"

you've assigned a string to x. So if you then type the expression:

x == "0"

into irb, it should respond:

=> true

since x *does* equal the string "0". If you instead type the expression:

x == 0

into irb, I would have expected irb to respond:

=> false

but it instead responds:

=> nil

and this admittedly surprised me :( But it does explain the result you
got when you typed:

puts "YES" if x == 0

into irb. The interpreter sees that x is not equal to the number zero,
and so it never calls puts. So the value of the last expression
evaluated is the value of (x == 0), which we've already seen is 'nil'.



Neither. I think Perl does this kind of implicit conversion (right?) but
Ruby does not.

Hope this helps,

Lyle

Thank you for your response Lyle. If ruby doesn't implicitly do
conversion, shouldn't the line puts "YES" if x == 0 generate
an interpreter error since x is a string type and 0 is a fixnum?
 
T

Tim Hunter

If you instead type the expression:

x == 0

into irb, I would have expected irb to respond:

=> false

but it instead responds:

=> nil

and this admittedly surprised me :(

IIRC, in 1.8.0, <=> returns nil when the operands are in different
classes. false is reserved for the case when both operands are in the same
class but have different values.
 
J

Joe Johnson

Tim said:
IIRC, in 1.8.0, <=> returns nil when the operands are in different
classes. false is reserved for the case when both operands are in the same
class but have different values.

Hmm.... I don't get nil when I compare different types. Any thoughts?

irb(main):001:0> 1==1
=> true
irb(main):002:0> 1=="1"
=> false
irb(main):003:0> 1=="3"
=> false
irb(main):004:0> "3"==3
=> false
irb(main):005:0> x="3"
=> "3"
irb(main):006:0> x==3
=> false
 
T

Tim Hunter

Hmm.... I don't get nil when I compare different types. Any thoughts?

irb(main):001:0> 1==1
=> true
irb(main):002:0> 1=="1"
=> false
irb(main):003:0> 1=="3"
=> false
irb(main):004:0> "3"==3
=> false
irb(main):005:0> x="3"
=> "3"
irb(main):006:0> x==3
=> false


I think it works for me...

[tim:~/rb]$ irb
irb(main):001:0> 1==1
=> true
irb(main):002:0> 1=="1"
=> nil
irb(main):003:0> 1=="3"
=> nil
irb(main):004:0> "3"==3
=> nil
irb(main):005:0> x="3"
=> "3"
irb(main):006:0> x==3
=> nil
irb(main):007:0> VERSION
=> "1.8.0"
irb(main):008:0>

[tim:~/rb]$ irb -v
irb 0.9(02/07/03)
[tim:~/rb]$ ruby -v
ruby 1.8.0 (2003-08-04) [i686-linux]
[tim:~/rb]$
 
J

Joe Johnson

Tim said:
Hmm.... I don't get nil when I compare different types. Any thoughts?

irb(main):001:0> 1==1
=> true
irb(main):002:0> 1=="1"
=> false
irb(main):003:0> 1=="3"
=> false
irb(main):004:0> "3"==3
=> false
irb(main):005:0> x="3"
=> "3"
irb(main):006:0> x==3
=> false



I think it works for me...

[tim:~/rb]$ irb
irb(main):001:0> 1==1
=> true
irb(main):002:0> 1=="1"
=> nil
irb(main):003:0> 1=="3"
=> nil
irb(main):004:0> "3"==3
=> nil
irb(main):005:0> x="3"
=> "3"
irb(main):006:0> x==3
=> nil
irb(main):007:0> VERSION
=> "1.8.0"
irb(main):008:0>

[tim:~/rb]$ irb -v
irb 0.9(02/07/03)
[tim:~/rb]$ ruby -v
ruby 1.8.0 (2003-08-04) [i686-linux]
[tim:~/rb]$

Oh.. I see, my have version 1.6.8. I bet
the behavior has changed. Has this been documented somewhere?
 
K

Kent Dahl

Joe said:
Thank you for your response Lyle. If ruby doesn't implicitly do
conversion, shouldn't the line puts "YES" if x == 0 generate
an interpreter error since x is a string type and 0 is a fixnum?

Interpreter error, such as at compile time? That would seem to imply
static typing, which Ruby thankfully does not have.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top