String Comparison Confusion

Discussion in 'Ruby' started by Joe Johnson, Oct 21, 2003.

  1. Joe Johnson

    Joe Johnson Guest

    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, Oct 21, 2003
    #1
    1. Advertising

  2. Joe Johnson

    Joe Johnson Guest

    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?
    Joe Johnson, Oct 22, 2003
    #2
    1. Advertising

  3. Joe Johnson

    Tim Hunter Guest

    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.
    Tim Hunter, Oct 22, 2003
    #3
  4. Joe Johnson

    Joe Johnson Guest

    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
    Joe Johnson, Oct 22, 2003
    #4
  5. Joe Johnson

    Tim Hunter Guest

    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]$
    Tim Hunter, Oct 22, 2003
    #5
  6. Joe Johnson

    Joe Johnson Guest

    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?
    Joe Johnson, Oct 22, 2003
    #6
  7. Joe Johnson

    Kent Dahl Guest

    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___)
    Kent Dahl, Oct 22, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Yakovenko

    Set and {} comparison confusion

    Roman Yakovenko, Sep 9, 2004, in forum: Python
    Replies:
    3
    Views:
    290
    Alex Martelli
    Sep 9, 2004
  2. Roman Yakovenko

    RE: Set and {} comparison confusion

    Roman Yakovenko, Sep 9, 2004, in forum: Python
    Replies:
    2
    Views:
    256
    Heiko Wundram
    Sep 9, 2004
  3. Roman Yakovenko

    FW: Set and {} comparison confusion

    Roman Yakovenko, Sep 9, 2004, in forum: Python
    Replies:
    1
    Views:
    262
    Alex Martelli
    Sep 9, 2004
  4. Replies:
    2
    Views:
    276
    Ben Finney
    Jan 20, 2007
  5. Deepu
    Replies:
    1
    Views:
    217
    ccc31807
    Feb 7, 2011
Loading...

Share This Page