Array #== and #===

Now I’m really confused:

irb(main):001:0> o = Object.new
=> #Object:0x2838c08
irb(main):002:0> o.class == Object
=> true
irb(main):003:0> o.class === Object
=> true
irb(main):004:0> o = Array.new
=> []
irb(main):005:0> o.class == Array
=> true
irb(main):006:0> o.class === Array
=> false

What’s going on here? Why is o.class === Array false?

Ruby version 1.8.0.

···

John Long
www.wiseheartdesign.com

John W. Long wrote:

irb(main):004:0> o = Array.new
=>
irb(main):006:0> o.class === Array
=> false
What’s going on here? Why is o.class === Array false?

I think you want this:

irb(main):007:0> Array === o
=> true

See “ri Class#===” for further details.

Regards,
Florian Gross

‘===’ is asking if the rhs is an instance of lhs so:

~ > irb
irb(main):001:0> Array.instance_of? Array
=> false
irb(main):002:0> Array === Array
=> false
irb(main):003:0> Array == Array
=> true
irb(main):004:0> .instance_of? Array
=> true
irb(main):005:0> Array ===
=> true

-a

···

On Thu, 1 Jan 2004, John W. Long wrote:

Date: Thu, 1 Jan 2004 04:03:39 +0900
From: John W. Long ws@johnwlong.com
Newsgroups: comp.lang.ruby
Subject: Array #== and #===

Now I’m really confused:

irb(main):001:0> o = Object.new
=> #Object:0x2838c08
irb(main):002:0> o.class == Object
=> true
irb(main):003:0> o.class === Object
=> true
irb(main):004:0> o = Array.new
=>
irb(main):005:0> o.class == Array
=> true
irb(main):006:0> o.class === Array
=> false

What’s going on here? Why is o.class === Array false?

ATTN: please update your address books with address below!

===============================================================================

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
STP :: Solar-Terrestrial Physics Data | NCEI
NGDC :: http://www.ngdc.noaa.gov/
NESDIS :: http://www.nesdis.noaa.gov/
NOAA :: http://www.noaa.gov/
US DOC :: http://www.commerce.gov/

The difference between art and science is that science is what we
understand well enough to explain to a computer.
Art is everything else.
– Donald Knuth, “Discover”

/bin/sh -c ‘for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done’
===============================================================================

What’s going on here? Why is o.class === Array false?

I think you want this:

irb(main):007:0> Array === o
=> true

See “ri Class#===” for further details.

ri Class#=== doesn’t exist for 1.8.0 (I’m on windows). Could you explain
what you mean in further detail?

I’m actually trying to do a case statement:

case o.class
case Array
puts “Array”

else
puts “Something Else”
end

···


John Long
http://wiseheartdesign.com

ri Class#=== doesn’t exist for 1.8.0 (I’m on windows).

Try Module#=== instead.

Could you explain
what you mean in further detail?

I’m actually trying to do a case statement:

case o.class
case Array
puts “Array”

else
puts “Something Else”
end

#=== is really all about case statements, so it’s already optimized for
what you’re trying to do. It’ll do what you want if you just drop the
call to #class:

case o
when Array
puts “Array”
else
puts “Something Else”
end

HTH,

Nathaniel

<:((><

···

On Dec 31, 2003, at 15:25, John W. Long wrote:

ri Class#=== doesn’t exist for 1.8.0 (I’m on windows).

Try Module#=== instead.

That worked. Thanks.

#=== is really all about case statements, so it’s already optimized for
what you’re trying to do. It’ll do what you want if you just drop the
call to #class:

case o
when Array
puts “Array”
else
puts “Something Else”
end

That clears things up. I was confused as to which #=== was called. A little
code to demonstrate your point:

irb(main):005:0> obj = Object.new
=> #Object:0x282d2c8
irb(main):006:0> def obj.===(other)
irb(main):007:1> puts “called”
irb(main):008:1> end
=> nil
irb(main):009:0> case obj
irb(main):010:1> when 1
irb(main):011:1> end
=> nil
irb(main):012:0> case 1
irb(main):013:1> when obj
irb(main):014:1> end
called
=> nil

This explains half of my question. Why does Object.new.class === Object
return true (instead of false like Array.new.class === Array)?

···


John Long
http://wiseheartdesign.com

Because the class Object is an instance of an Object (as is everything
in Ruby), but the class Array is not an instance of an Array.
Basically, Module#=== is just a reversed version of Object#kind_of?, so
these two assertions are saying:

irb(main):001:0> Object.kind_of?(Object)
=> true
irb(main):002:0> Array.kind_of?(Array)
=> false

HTH,

Nathaniel

<:((><

···

On Dec 31, 2003, at 16:17, John W. Long wrote:

This explains half of my question. Why does Object.new.class === Object
return true (instead of false like Array.new.class === Array)?

so these two assertions are saying:

irb(main):001:0> Object.kind_of?(Object)
=> true
irb(main):002:0> Array.kind_of?(Array)
=> false

I see. Thanks.

···


John Long
www.wiseheartdesign.com