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

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

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?

The class Class has no subclasses:

   irb(main):004:0> class C < Class; end
   TypeError: can't make subclass of Class

Every class is an instance of Class itself, including singleton
classes of all flavors.

You can test this like this:

   class C
     class << self
       puts self.class
     end
   end

   => Class

What happens with inheritance is this: Given the following:

   class C
   end

   class D < C
   end

C is the superclass of D, and C's singleton class (yes, I prefer to
avoid the term metaclass entirely :slight_smile: is the superclass of D's
singleton class. So it's like this, looking at it side by side:

   class C class C'
   end end

   class D < C class D' < C'
   end end

where the thing on the right happens automatically.

You can verify this:

   irb(main):013:0> class << C; object_id; end
   537746792
   irb(main):014:0> class << D; superclass.object_id; end
   537746792

This, by the way, is why you can do this:

   def C.x; puts "A singleton method of C"; end
   D.x

    => "A singleton method of C"

It's strange for one object (D) to be able to call the singleton
methods of another (C) -- except that classes are treated as a special
case. So it's as if you'd done this:

   class C'
     def x
       puts "A singleton method of C"
     end
   end

   class D' < C'
   end

So "instances" of D' will respond to that x method -- and the only
"instance" of D', by definition, is D itself.

David

···

On Sun, 21 May 2006, uncutstone wu wrote:

--
David A. Black (dblack@wobblini.net)
* Ruby Power and Light, LLC (http://www.rubypowerandlight.com)
   > Ruby and Rails consultancy and training
* Author of "Ruby for Rails" from Manning Publications!
   > Ruby for Rails

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

···

--
Posted via http://www.ruby-forum.com/\.

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?

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

···

--
Posted via http://www.ruby-forum.com/\.

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 Weirich

···

--
Posted via http://www.ruby-forum.com/\.

  Why AClass.metaclass.superclass gets "#<Class:Object>"?

What do you expect ?

  Why ancestor's doesn't include superclass?

#ancestors don't include singleton classes

Guy Decoux

ts wrote:

> Why AClass.metaclass.superclass gets "#<Class:Object>"?

What do you expect ?

> Why ancestor's doesn't include superclass?

#ancestors don't include singleton classes

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

uncutstone

···

--
Posted via http://www.ruby-forum.com/\.

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

ts wrote:

> 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

···

--
Posted via http://www.ruby-forum.com/\.

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

ts wrote:

> 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

···

--
Posted via http://www.ruby-forum.com/\.

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

ts wrote:

> 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

···

--
Posted via http://www.ruby-forum.com/\.