[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

class method

Spitfire

12/22/2006 10:16:00 AM

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???)

--
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
?I'm smart enough to know that I'm dumb.?
- Richard P Feynman
12 Answers

Peter Szinek

12/22/2006 10:22:00 AM

0

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.rubyra...



Eric Hodel

12/22/2006 10:29:00 AM

0

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.se...

I LIT YOUR GEM ON FIRE!


Gregor Kopp

12/22/2006 10:32:00 AM

0

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

Gregor Kopp

12/22/2006 10:34:00 AM

0

Gregor Kopp schrieb:
> 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

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

Spitfire

12/22/2006 10:34:00 AM

0

Gregor Kopp wrote:
>> sample = [1,2,3]
>> p sample.class #=> Array
>> p sample.is_a?(Array) #=> true
>
> isa? makes checks with .class in the Background automagically.
so what is exactly happening when I type
sample.class.is_a?...


--
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
?I'm smart enough to know that I'm dumb.?
- Richard P Feynman

Rimantas Liubertas

12/22/2006 10:38:00 AM

0

> 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
--
http://rim...

Gregor Kopp

12/22/2006 10:46:00 AM

0

Spitfire schrieb:
> Gregor Kopp wrote:
>
>>> sample = [1,2,3]
>>> p sample.class #=> Array
>>> p sample.is_a?(Array) #=> true
>>
>>
>> isa? makes checks with .class in the Background automagically.
>
> so what is exactly happening when I type
> sample.class.is_a?...
>
>

i think it does a check with samle.class.class in the Background wich
results in Class

irb(main):001:0> class A
irb(main):002:1> end
=> nil
irb(main):003:0> a = A.new
=> #<A:0x2f8b4b4>
irb(main):004:0> a.class
=> A
irb(main):005:0> a.class.is_a? A
=> false
irb(main):006:0> a.class.class
=> Class
irb(main):007:0> A.class
=> Class

Spitfire

12/22/2006 10:50:00 AM

0

Gregor Kopp wrote:
> Spitfire schrieb:
>> Gregor Kopp wrote:
>>
>>>> sample = [1,2,3]
>>>> p sample.class #=> Array
>>>> p sample.is_a?(Array) #=> true
>>>
>>>
>>> isa? makes checks with .class in the Background automagically.
>>
>> so what is exactly happening when I type
>> sample.class.is_a?...
>>
>>
>
> i think it does a check with samle.class.class in the Background wich
> results in Class
>
> irb(main):001:0> class A
> irb(main):002:1> end
> => nil
> irb(main):003:0> a = A.new
> => #<A:0x2f8b4b4>
> irb(main):004:0> a.class
> => A
isn't this 'A' the class 'A'???
if so when I'm checking for a type of this, it should return true right???

I'm sorry if I'm still not grasping the concept.

> irb(main):005:0> a.class.is_a? A
> => false
this still confuses me!

--
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
?I'm smart enough to know that I'm dumb.?
- Richard P Feynman

Peter Szinek

12/22/2006 11:00:00 AM

0


>> irb(main):005:0> a.class.is_a? A
>> => false
> this still confuses me!

a 'is-an' A. you ask for this with

a.is_a? A

When you are asking

a.class.is_a? A

that actually means 'A.class is_a? A' which is not true since A.class is
Class.

Peter

__
http://www.rubyra...



Spitfire

12/22/2006 11:05:00 AM

0

Peter Szinek wrote:
> a 'is-an' A. you ask for this with
>
> a.is_a? A
>
> When you are asking
>
> a.class.is_a? A
>
> that actually means 'A.class is_a? A' which is not true since A.class is
> Class.
now I get it! Thanks. I appreciate the help.

--
_ _ _]{5pitph!r3}[_ _ _
__________________________________________________
?I'm smart enough to know that I'm dumb.?
- Richard P Feynman