Why Fixnum===Fixnum is false?

H

Heesob Park

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
 
J

Joel VanderWerf

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
 
A

Adam Gardner

Joel said:
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top