Forum: Ruby class method

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.
0ed1fc95a73b8626b25b6806ed7497f1?d=identicon&s=25 Spitfire (Guest)
on 2006-12-22 11:21
(Received via mailing list)
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???)
F50f5d582d76f98686da34917531fe56?d=identicon&s=25 Peter Szinek (Guest)
on 2006-12-22 11:23
(Received via mailing list)
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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-12-22 11:31
(Received via mailing list)
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 Hodel - drbrain@segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!
97187d7e48d59c178c7af8eaaaa3857c?d=identicon&s=25 Gregor Kopp (Guest)
on 2006-12-22 11:31
(Received via mailing list)
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
97187d7e48d59c178c7af8eaaaa3857c?d=identicon&s=25 Gregor Kopp (Guest)
on 2006-12-22 11:37
(Received via mailing list)
Gregor Kopp schrieb:
> sample = [1,2,3]
> p sample.class                #=> Array
> p sample.is_a?(Array)         #=> true

isa? makes checks with .class in the Background automagically.
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-12-22 11:39
(Received via mailing list)
>         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
This topic is locked and can not be replied to.