About <programming ruby, 2nc edition>, meta class and Class


#1

General speaking, itâ??s about the relationship between virtual
class(metaclass ) and Class.

Itâ??s all in chapter 24.
In Figure 24.2, Dave gives the relationship between object
instance(a_guitar), its class(Guitar) and its metaclass(Guitar). . Then in section â??Class and Module Definitionsâ? , as we see in the code example, any class (for example, class Test) is a object instance of class Class. So I think Class is a metaclass of any class. Since Guitar is a
metaclass of Guitar, can I think Guitar` is a subclass of Class. If my
understanding is right, I think itâ??s better Dave can add this
relationship in Figure 24.2 and make it clearer.

Thanks in advance

uncutstone


#2

The following is quoted from ruby-1.8.4-core-rdocs

Classes in Ruby are first-class objectsâ??each is an instance of class Class.
All metaclasses are instances of the class `Classâ??.

This makes me confused. In my understanding, all metaclasses are
subclass of the class ‘Class’ as I posted in previous post.

Somebody can explain it?

Thanks in advance.

uncutstone


#3

uncutstone wu wrote:

The following is quoted from ruby-1.8.4-core-rdocs

Classes in Ruby are first-class objectsâ??each is an instance of class Class.
All metaclasses are instances of the class `Classâ??.

This makes me confused. In my understanding, all metaclasses are
subclass of the class ‘Class’ as I posted in previous post.

My new understanding is: any metaclass(singleton class of a class
object) is a subclass of the class ‘Class’ and also a instance of the
class ‘Class’. Am I right?

I write a snippet of code to test this understanding:

class Object
def metaclass
class <<self
self
end
end
end

class AClass
end

puts AClass.metaclass.class
puts AClass.metaclass.superclass
puts AClass.metaclass.ancestors

the result:
Class
#Class:Object
Class
Module
Object
Kernel
It almost proves my understanding but there is still something I don’t
fully understand. It is line 2 of the result, say, #Class:Ojbect.

Somebody can explain?

Thanks in advance.

uncutstone


#4

Maybe this helps:

/*
 * Ruby's Class Hierarchy Chart
 *
 *                           +------------------+
 *                           |                  |
 *             Object---->(Object)              |
 *              ^  ^        ^  ^                |
 *              |  |        |  |                |
 *              |  |  +-----+  +---------+      |
 *              |  |  |                  |      |
 *              |  +-----------+         |      |
 *              |     |        |         |      |
 *       +------+     |     Module--->(Module)  |
 *       |            |        ^         ^      |
 *  OtherClass-->(OtherClass)  |         |      |
 *                             |         |      |
 *                           Class---->(Class)  |
 *                             ^                |
 *                             |                |
 *                             +----------------+
 *
 *   + All metaclasses are instances of the class `Class'.
 */

T.

P.S. Recently noticed Smalltalk uses the term metaclass. I wonder why
singleton took it’s place?


#5

unknown wrote:

P.S. Recently noticed Smalltalk uses the term metaclass. I wonder why
singleton took it’s place?

Only classes have metaclasses, but any object can have a singleton
class. And although singleton classes of classes are similar to
metaclasses in some ways, they are not quite the same thing as the
Smalltalk metaclasses.

– Jim W.


#6

uncutstone wu wrote:

puts AClass.metaclass.class
puts AClass.metaclass.superclass
puts AClass.metaclass.ancestors

the result:
Class
#Class:Object
Class
Module
Object
Kernel

Can somebody explain:
Why AClass.metaclass.superclass gets “#Class:Object”?
Why AClass.metaclass.ancestors gets “[Class,Module,Object,Kernel]”?
Acctually, I understand this one.
Why ancestor’s doesn’t include superclass?

Thanks in advance.

uncutstone


#7

“u” == uncutstone wu removed_email_address@domain.invalid writes:

u> Why AClass.metaclass.superclass gets “#Class:Object”?

What do you expect ?

u> Why ancestor’s doesn’t include superclass?

#ancestors don’t include singleton classes

Guy Decoux


#8

“u” == uncutstone wu removed_email_address@domain.invalid writes:

u> Thanks very much. I think I already understand it now.

Well, if you understand it, try it with ruby 1.8.4 :slight_smile:

You use ruby 1.8.2, no ?

Guy Decoux


#9

ts wrote:

“u” == uncutstone wu removed_email_address@domain.invalid writes:

u> Why AClass.metaclass.superclass gets “#Class:Object”?

What do you expect ?

u> Why ancestor’s doesn’t include superclass?

#ancestors don’t include singleton classes

Thanks very much. I think I already understand it now.

uncutstone


#10

“u” == uncutstone wu removed_email_address@domain.invalid writes:

u> I think it is because 1.8.2 and 1.8.4 have different object models.

no, the object model is not changed.

the problem is here

moulon% ./ruby -ve ‘a = Object.new; p class << a; self end.superclass’
ruby 1.8.2 (2004-12-25) [i686-linux]
Object
moulon%

moulon% /usr/bin/ruby -ve ‘a = Object.new; p class << a; self
end.superclass’
ruby 1.8.4 (2005-12-24) [i486-linux]
#Class:Object
moulon%

moulon% ./ruby -ve ‘class A < Object; end; p class << A; self
end.superclass’
ruby 1.8.2 (2004-12-25) [i686-linux]
#Class:Object
moulon%

moulon% /usr/bin/ruby -ve ‘class A < Object; end; p class << A; self
end.superclass’
ruby 1.8.4 (2005-12-24) [i486-linux]
#Class:Class
moulon%

trying to correct a bug has perhaps introduced a problem

Guy Decoux


#11

ts wrote:

“u” == uncutstone wu removed_email_address@domain.invalid writes:

u> Thanks very much. I think I already understand it now.

Well, if you understand it, try it with ruby 1.8.4 :slight_smile:

You use ruby 1.8.2, no ?

Guy Decoux

Yes, I use 1.8.2.
And I try it in 1.8.4 and get “#Class:Class” from
AClass.metaclass.superclass instead of “#Class:Object”.

I think it is because 1.8.2 and 1.8.4 have different object models.
What is the defference between object models of ruby 1.8.2 and 1.8.4?
Where can I find this kind of information?

Thanks in advance.

uncutstone


#12

ts wrote:

“u” == uncutstone wu removed_email_address@domain.invalid writes:

u> I think it is because 1.8.2 and 1.8.4 have different object models.

no, the object model is not changed.

the problem is here

moulon% ./ruby -ve ‘a = Object.new; p class << a; self end.superclass’
ruby 1.8.2 (2004-12-25) [i686-linux]
Object
moulon%

moulon% /usr/bin/ruby -ve ‘a = Object.new; p class << a; self
end.superclass’
ruby 1.8.4 (2005-12-24) [i486-linux]
#Class:Object
moulon%

moulon% ./ruby -ve ‘class A < Object; end; p class << A; self
end.superclass’
ruby 1.8.2 (2004-12-25) [i686-linux]
#Class:Object
moulon%

moulon% /usr/bin/ruby -ve ‘class A < Object; end; p class << A; self
end.superclass’
ruby 1.8.4 (2005-12-24) [i486-linux]
#Class:Class
moulon%

trying to correct a bug has perhaps introduced a problem

So, you mean this is a bug of version 1.8.4.

Best Regards,

uncutstone


#13

ts wrote:

“u” == uncutstone wu removed_email_address@domain.invalid writes:

u> So, you mean this is a bug of version 1.8.4.

Well, for me this is strange

moulon% ./ruby -ve ‘class A < Object; end; p class << A; self
end.superclass’
ruby 1.8.4 (2006-06-19) [i686-linux]
#Class:Class
moulon%

I prefer when ruby say this

moulon% ./ruby -ve ‘class A < Object; end; p class << A; self
end.superclass’
ruby 1.9.0 (2006-06-20) [i686-linux]
#Class:Object
moulon%

Guy Decoux

It’s reasonable. I agree with you.

Best regards,

uncutstone


#14

“u” == uncutstone wu removed_email_address@domain.invalid writes:

u> So, you mean this is a bug of version 1.8.4.

Well, for me this is strange

moulon% ./ruby -ve ‘class A < Object; end; p class << A; self
end.superclass’
ruby 1.8.4 (2006-06-19) [i686-linux]
#Class:Class
moulon%

I prefer when ruby say this

moulon% ./ruby -ve ‘class A < Object; end; p class << A; self
end.superclass’
ruby 1.9.0 (2006-06-20) [i686-linux]
#Class:Object
moulon%

Guy Decoux