Class method


#1

One thing that I don’t understand is the following code.

sample = [1,2,3]
p sample.class				#=> Array
p sample.class.is_a?(Array) 		#=> false
p sample.class.is_a?(Array.class) 	#=> true   (why???)

#2

Spitfire wrote:

One thing that I don’t understand is the following code.

sample = [1,2,3]
p sample.class                #=> Array
p sample.class.is_a?(Array)         #=> false
p sample.class.is_a?(Array.class)     #=> true   (why???)

Hmm, mayvbe because Array.class is Class and sample.class is a Class?

Cheers,
Peter

__
http://www.rubyrailways.com


#3

On Dec 22, 2006, at 02:20, Spitfire wrote:

One thing that I don’t understand is the following code.

sample = [1,2,3]
p sample.class #=> Array
p sample.class.is_a?(Array) #=> false
p sample.class.is_a?(Array.class) #=> true (why???)

$ ruby
sample = [1, 2, 3]
p sample.class
p Array.class
p sample.class.is_a?(Array.class)
Array
Class
true

therefore:

$ ruby
p Array.is_a?(Class)
true

Sensible.


Eric H. - removed_email_address@domain.invalid - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!


#4

Spitfire schrieb:

One thing that I don’t understand is the following code.

sample = [1,2,3]
p sample.class                #=> Array
p sample.class.is_a?(Array)         #=> false
p sample.class.is_a?(Array.class)     #=> true   (why???)

sample = [1,2,3]
p sample.class #=> Array
p sample.is_a?(Array) #=> true


#5

Gregor K. schrieb:

sample = [1,2,3]
p sample.class #=> Array
p sample.is_a?(Array) #=> true

isa? makes checks with .class in the Background automagically.


#6
    sample = [1,2,3]
    p sample.class                          #=> Array
    p sample.class.is_a?(Array)             #=> false

Sure. sample.class is Array and Array.class is Class.
Sample.class.is_a? (Array) means mostly the same as “is the class of
sample.class Array?”. Becaue class of the sample.class is Array not
Class you get false here.

But: sample.is_a? (Array) #=> true

    p sample.class.is_a?(Array.class)       #=> true   (why???)

Class is Class.

I guess you need to understand the difference between sample.is_a? and
sample.class.is_a?

Regards,
Rimantas