Why Fixnum===Fixnum is false?

Discussion in 'Ruby' started by Heesob Park, May 13, 2009.

  1. Heesob Park

    Heesob Park Guest

    Hi,

    I noticed the following behaviour.

    irb(main):001:0> a = 1
    => 1
    irb(main):002:0> a.class
    => Fixnum
    irb(main):003:0> a.class == Fixnum
    => true
    irb(main):004:0> a.class === Fixnum
    => false
    irb(main):005:0> Fixnum === Fixnum
    => false
    irb(main):006:0> a === Fixnum
    => false
    irb(main):007:0> Fixnum === a
    => true

    As a result:

    case 1
    when Fixnum
    puts 'Fixnum'
    else
    puts 'else'
    end
    # => Fixnum

    case 1.class
    when Fixnum
    puts 'Fixnum'
    else
    puts 'else'
    end
    #=> else

    What is the reason Fixnum === Fixnum returns false?

    Regards,
    Park Heesob
     
    Heesob Park, May 13, 2009
    #1
    1. Advertising

  2. Fixnum is not an instance of Fixnum. Compare:

    ------------------------------------------------------------- Object#===
    obj === other => true or false

    From Ruby 1.8
    ------------------------------------------------------------------------
    Case Equality---For class Object, effectively the same as calling
    #==, but typically overridden by descendents to provide meaningful
    semantics in case statements.

    ------------------------------------------------------------- Module#===
    mod === obj => true or false

    From Ruby 1.8
    ------------------------------------------------------------------------
    Case Equality---Returns true if anObject is an instance of mod or
    one of mod's descendents. Of limited use for modules, but can be
    used in case statements to classify objects by class.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, May 13, 2009
    #2
    1. Advertising

  3. Heesob Park

    Adam Gardner Guest

    Joel VanderWerf wrote:
    > Fixnum is not an instance of Fixnum. Compare:
    >
    > ------------------------------------------------------------- Object#===
    > obj === other => true or false
    >
    > From Ruby 1.8
    > ------------------------------------------------------------------------
    > Case Equality---For class Object, effectively the same as calling
    > #==, but typically overridden by descendents to provide meaningful
    > semantics in case statements.
    >
    > ------------------------------------------------------------- Module#===
    > mod === obj => true or false
    >
    > From Ruby 1.8


    Uh, I think you omitted the most important part of the documentation, to
    whit, what it actually says about Module#===:

    ------------------------------------------------------------- Module#===
    mod === obj => true or false
    ------------------------------------------------------------------------
    Case Equality---Returns +true+ if _anObject_ is an instance of
    _mod_ or one of _mod_'s descendents. Of limited use for modules,
    but can be used in +case+ statements to classify objects by class.

    (and incidentally, it is also important to know that the class 'Class'
    does not define it's own #=== method, and so inherits from Module, which
    is hinted at but not explicitly stated here)
    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Gardner, May 13, 2009
    #3
  4. Adam Gardner wrote:
    > Joel VanderWerf wrote:
    >> Fixnum is not an instance of Fixnum. Compare:

    ...
    > Uh, I think you omitted the most important part of the documentation, to
    > whit, what it actually says about Module#===:


    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/336430

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, May 13, 2009
    #4
  5. Heesob Park

    Adam Gardner Guest

    Adam Gardner, May 14, 2009
    #5
  6. Adam Gardner wrote:
    > Joel VanderWerf wrote:
    >> Adam Gardner wrote:
    >>> Joel VanderWerf wrote:
    >>>> Fixnum is not an instance of Fixnum. Compare:

    >> ...
    >>> Uh, I think you omitted the most important part of the documentation, to
    >>> whit, what it actually says about Module#===:

    >> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/336430

    >
    > Huh, odd. Bug in Ruby-forum, then, I guess. Sorry.
    >
    > ( http://www.ruby-forum.com/topic/186753#815590 )


    Hm, it probably thought everything below the last hline was a sig.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, May 14, 2009
    #6
    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. André
    Replies:
    3
    Views:
    1,618
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    922
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,076
    Smokey Grindel
    Dec 2, 2006
  4. Rajarshi

    0 == False but [] != False?

    Rajarshi, May 24, 2007, in forum: Python
    Replies:
    20
    Views:
    719
    Erik Max Francis
    May 30, 2007
  5. MaggotChild
    Replies:
    6
    Views:
    146
    MaggotChild
    Dec 2, 2009
Loading...

Share This Page