Array !== Array?

What is the rationale behind Array === Array (or indeed any other class) being
false? Array.new === Array is also false. How do I test what class some
object is in a case statement?

Tim Bates

···


tim@bates.id.au

Tim Bates wrote:

What is the rationale behind Array === Array (or indeed any other class) being
false?

It’s the case equality operator. The Array class object isn’t a case of
Array, its a case of Class.

Array.new === Array is also false.

Array === Array.new #=> true

How do I test what class some
object is in a case statement?

The case statement reverses the order of the call when calling the ===.

case Array.new
when Array
puts “I’m an Array”
end

outputs “I’m an Array”

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

irb(main):001:0> a =

irb(main):002:0> case a
irb(main):003:1> when Array
irb(main):004:1> puts “Array”
irb(main):005:1> when Hash
irb(main):006:1> puts “Hash”
irb(main):007:1> end
Array
nil

ie, in
case a
when B

when C

end
the comparison being done is B === a, not a === B.
It just makes sense that
irb(main):008:0> Array === Array.new
true

And of course
irb(main):009:0> Array === Array
false

because Array is not an instance of class Array but of class Class
irb(main):010:0> Class === Array
true

···

On Mon, Dec 02, 2002 at 07:20:05PM +0900, Tim Bates wrote:

What is the rationale behind Array === Array (or indeed any other class) being
false? Array.new === Array is also false. How do I test what class some
object is in a case statement?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

win-nt from the people who invented edlin.
– MaDsen Wikholm, mwikholm@at8.abo.fi