Velocity Reviews > Ruby > String Comparison Confusion

# String Comparison Confusion

Joe Johnson
Guest
Posts: n/a

 10-21-2003
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

Joe Johnson
Guest
Posts: n/a

 10-21-2003
Lyle Johnson wrote:
> Joe Johnson wrote:
>
>> Hi, can someone please explain why the line
>> puts "YES" if x == 0 returns nil?

>
>
> 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
>
>
> => 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'.
>
>> Does ruby convert x to a fixnum or convert 0 to a string?

>
>
> 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?

Tim Hunter
Guest
Posts: n/a

 10-21-2003
On Tue, 21 Oct 2003 17:47:20 -0500, Lyle Johnson wrote:

> If you instead type the expression:
>
> x == 0
>
> into irb, I would have expected irb to respond:
>
> => false
>
>
> => 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.

Joe Johnson
Guest
Posts: n/a

 10-21-2003
Tim Hunter wrote:
> On Tue, 21 Oct 2003 17:47:20 -0500, Lyle Johnson wrote:
>
>
>>If you instead type the expression:
>>
>> x == 0
>>
>>into irb, I would have expected irb to respond:
>>
>> => false
>>
>>
>> => nil
>>

>
>
> 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

Tim Hunter
Guest
Posts: n/a

 10-22-2003
On Tue, 21 Oct 2003 16:58:49 -0700, Joe Johnson wrote:

> Tim Hunter wrote:
>> On Tue, 21 Oct 2003 17:47:20 -0500, Lyle Johnson wrote:
>>
>>
>>>If you instead type the expression:
>>>
>>> x == 0
>>>
>>>into irb, I would have expected irb to respond:
>>>
>>> => false
>>>
>>>
>>> => nil
>>>

>>
>>
>> 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

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]\$

Joe Johnson
Guest
Posts: n/a

 10-22-2003
Tim Hunter wrote:
> On Tue, 21 Oct 2003 16:58:49 -0700, Joe Johnson wrote:
>
>
>>Tim Hunter wrote:
>>
>>>On Tue, 21 Oct 2003 17:47:20 -0500, Lyle Johnson wrote:
>>>
>>>
>>>
>>>>If you instead type the expression:
>>>>
>>>> x == 0
>>>>
>>>>into irb, I would have expected irb to respond:
>>>>
>>>> => false
>>>>
>>>>
>>>> => nil
>>>>
>>>
>>>
>>>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

>
>
>
> 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?

Kent Dahl
Guest
Posts: n/a

 10-22-2003
Joe Johnson wrote:
> 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.

--
(\[ Kent Dahl ]/)_ _~_ _____[ http://www.pvv.org/~kentda/ ]_____/~
))\_student_/(( \__d L b__/ Master of Science in Technology )
( \__\_õ|õ_/__/ ) _)Industrial economics and technological management(
\____/_ö_\____/ (____engineering.discipline_=_Computer::Technology ___)