Forum: Ruby Why Fixnum===Fixnum is false?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-05-13 03:20
(Received via mailing list)
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-05-13 03:49
(Received via mailing list)
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
Eacf5ed623e72db0b87707baabd6f21a?d=identicon&s=25 Adam Gardner (adamgardner)
on 2009-05-13 19:02
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)
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-05-13 19:20
(Received via mailing list)
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/...
Eacf5ed623e72db0b87707baabd6f21a?d=identicon&s=25 Adam Gardner (adamgardner)
on 2009-05-14 02:00
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/...

Huh, odd. Bug in Ruby-forum, then, I guess. Sorry.

( http://www.ruby-forum.com/topic/186753#815590 )
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-05-14 02:32
(Received via mailing list)
Adam Gardner wrote:
>
> ( http://www.ruby-forum.com/topic/186753#815590 )

Hm, it probably thought everything below the last hline was a sig.
This topic is locked and can not be replied to.