Class method

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

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

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

···

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

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

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

        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://rimantas.com/

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.

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

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

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

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.rubyrailways.com

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

Hi --

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.

That's not quite right. a.class doesn't test A.class; it tests A. So
this:

   a.class.is_a? A

is the same as:

   A.is_a? A

which is not true.

David

···

On Fri, 22 Dec 2006, Peter Szinek wrote:

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Also, the Pick Axe book has a section on the ruby object model that I thought was really helpful learning this stuff. There's a dichotomy between instance and class objects that I wasn't aware of until I read it.
-Mat

···

On Dec 22, 2006, at 6:10 AM, Spitfire wrote:

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.