class method

S

Spitfire

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

Peter Szinek

Spitfire said:
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
 
E

Eric Hodel

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

Gregor Kopp

Spitfire said:
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
 
G

Gregor Kopp

Gregor said:
Spitfire said:
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.
 
R

Rimantas Liubertas

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
 
G

Gregor Kopp

Spitfire said:
Gregor said:
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
 
S

Spitfire

Gregor said:
Spitfire said:
Gregor said:
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!
 
S

Spitfire

Peter said:
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.
 
M

Mat Schaffer

now I get it! Thanks. I appreciate the help.

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
 
D

dblack

Hi --

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top