[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String Comparison Confusion

Joe Johnson

10/21/2003 10:11:00 PM

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

8 Answers

Lyle Johnson

10/21/2003 10:47:00 PM

0

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

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

> 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

Joe Johnson

10/21/2003 11:11:00 PM

0

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

10/21/2003 11:12:00 PM

0

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

Joe Johnson

10/21/2003 11:59:00 PM

0

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

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

10/22/2003 12:35:00 AM

0

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

10/22/2003 2:09:00 AM

0

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

10/22/2003 7:48:00 AM

0

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.or... ]_____/~
))\_student_/(( \__d L b__/ Master of Science in Technology )
( \__\_õ|õ_/__/ ) _)Industrial economics and technological management(
\____/_ö_\____/ (____engineering.discipline_=_Computer::Technology___)

Lyle Johnson

10/22/2003 3:49:00 PM

0

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?

I don't think there's a right or wrong answer; it just boils down to a
language design decision, whether comparing two objects of different
types should raise an exception or merely return nil, as Ruby does.