Metaclasses... 
Author Message
 Metaclasses...

Hello, all...

I've been trying to understand metaclasses
(mostly through a search of the ruby-talk archive).

I guess I have two questions:

1. Why does MyClass.class return Class, if the
class of MyClass is really MyClass's metaclass?

2. Why do we create a metaclass if all classes are
instances of Class? Couldn't Class have been used
as a generic metaclass?

Thanks,
Hal Fulton



Tue, 02 Nov 2004 04:49:22 GMT  
 Metaclasses...

Quote:
> 2. Why do we create a metaclass if all classes are
> instances of Class? Couldn't Class have been used
> as a generic metaclass?

At the risk of doing a Ron Jeffries, I'll answer with a question.
Where would MyClass's class methods live?

Dave



Tue, 02 Nov 2004 05:51:58 GMT  
 Metaclasses...
Hi --

Quote:

> ----- Original Message -----


> Sent: Thursday, May 16, 2002 4:47 PM
> Subject: Re: Metaclasses...


> > > 2. Why do we create a metaclass if all classes are
> > > instances of Class? Couldn't Class have been used
> > > as a generic metaclass?

> > At the risk of doing a Ron Jeffries, I'll answer with a question.
> > Where would MyClass's class methods live?

> Waxahachie?

> No, seriously... why couldn't they live in Class?

For one thing, if you had:

  class A
    def A.thing
    end
  end

  class B
    def B.thing
    end
  end

and if class << A and class << B were both Class, Class would have a
name conflict at #thing.

Quote:
> I'm probably being dense. The simple difference between
> "X is an instance of Y" and "X inherits from Y" confuses
> me.

> I have spent many hours staring glassy-eyed and
> muttering, "Class is an object... Object is a class...
> Object is an object... Class is a class..."

There's definitely a circularity at the top of the family tree, where
this stuff is bootstrapped.  One way to think about it is that, even
before you, as programmer, can use class Class to create a new Class
object, Ruby can create new Class objects internally.  So when you
wonder, how can the type of Object be Class, if Class is an Object,
you can think of it as: Ruby already knew how to create a class (such
as Object), prior to making the class Class available to you so that
you could create your own Class objects.

To quote [myself in :-] "Teach Yourself Ruby in 21 Days": "...even
though class Class is what enables you the programmer to create new
classes, it's really just a gateway to something that Ruby can already
do internally."

(Probably more of a conceptual aid than a technically rigorous
account... but hopefully useful.)

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Tue, 02 Nov 2004 06:43:58 GMT  
 Metaclasses...
Hi,

In message "Metaclasses..."

|I've been trying to understand metaclasses

You don't have to, because there's no such a thing in Ruby.
In Smalltalk, a metaclass is a class of a class.

In Ruby, the Class class is the class of all classes, no metaclass.
But every object (of course including classes) can have hidden
meta-object (the place holder for singleton methods), which is only
revealed by the "singleton class notation".

                                                        matz.



Tue, 02 Nov 2004 08:48:43 GMT  
 Metaclasses...

    Hal> I'm probably being dense. The simple difference between
    Hal> "X is an instance of Y" and "X inherits from Y" confuses
    Hal> me.

Yeah, I hear you.

The instance-of hierarcy is differenct and separate from the
inherits-from hierarchy.  The fact that we tend to use "is-a" for both
makes it confusing.

Try this.  Draw a diagram of the major players (Object, Class, etc.).
Mark all the instance-of relationships with one color, and the
inherits-from relationships with another color.  Study the diagram
real hard.  Study it real hard.  When you are done, you probably won't
understand it any better, but you will have a cool diagram to hang on
your wall. :-)

--

---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)



Tue, 02 Nov 2004 11:40:34 GMT  
 Metaclasses...

Quote:
----- Original Message -----


Sent: Thursday, May 16, 2002 7:47 PM
Subject: Re: Metaclasses...

> In message "Metaclasses..."

> |I've been trying to understand metaclasses

> You don't have to, because there's no such a thing in Ruby.
> In Smalltalk, a metaclass is a class of a class.

> In Ruby, the Class class is the class of all classes, no metaclass.
> But every object (of course including classes) can have hidden
> meta-object (the place holder for singleton methods), which is only
> revealed by the "singleton class notation".

I realize that Smalltalk and Ruby differ on that.

But these meta-objects do exist.

But this is very interesting: You say that their purpose is to hold
singleton methods. Is this truly the only purpose for them in Ruby?

Thanks,
Hal



Tue, 02 Nov 2004 11:56:12 GMT  
 Metaclasses...

Quote:

> Try this.  Draw a diagram of the major players (Object, Class, etc.).
> Mark all the instance-of relationships with one color, and the
> inherits-from relationships with another color.  Study the diagram
> real hard.  Study it real hard.  When you are done, you probably won't
> understand it any better, but you will have a cool diagram to hang on
> your wall. :-)

LOL!!!

One way to think about it is:

   If:
     Foo is a Class that inherits from Bar
     foo is an instance of Foo
   Then:
     foo is a Foo (instance-of)
     A Foo is a Bar (inheritance)
     foo is a Bar (instance-of)

So if you have to put an article in front of your noun, you are probably
talking inheritance.  Without the article, it's probably instance-of.
You can pretty easily apply this to the Class/Object/etc. relationships,
but I'll leave this as an exercise for the reader.

Paul



Tue, 02 Nov 2004 12:22:15 GMT  
 Metaclasses...
Hi,

In message "Re: Metaclasses..."

|But these meta-objects do exist.

In the current implementation, yes.  They need not to exist in the
other implementaions.

|But this is very interesting: You say that their purpose is to hold
|singleton methods. Is this truly the only purpose for them in Ruby?

It is its primary purpose.  You can use "meta-object" for other
purposes using "singleton class notation".  But I consider it abuse.
Notice singleton class notation did not exist in the first place,
whereas singleton methods did exist from the beginning.

                                                        matz.



Tue, 02 Nov 2004 12:43:07 GMT  
 Metaclasses...
I would like to set up a list server that is implemented  in Ruby
because I want to be able to hack it.

Is there such a thing available?

All I could find on RAA is quickmlserver and that is not down loadable.
I don't know if it reaches the level of Mailman.

What would people suggest as a mail server? Mailman is my current
favorite. But when I tell people about how wonderful  Ruby is. I cannot
forget that mailman is so good and it is written in Python.

John



Tue, 02 Nov 2004 12:53:34 GMT  
 Metaclasses...

Quote:

> I would like to set up a list server that is implemented  in Ruby
> because I want to be able to hack it.

> Is there such a thing available?

The Seattle Ruby Brigade is working on part of this, and would welcome
others interested in contributing.  Please see
www.rubygarden.org/ruby?SeattleGroupMarchProject for some information, or

more detail.

thanks,
-pate

Quote:

> All I could find on RAA is quickmlserver and that is not down loadable.
> I don't know if it reaches the level of Mailman.

> What would people suggest as a mail server? Mailman is my current
> favorite. But when I tell people about how wonderful  Ruby is. I cannot
> forget that mailman is so good and it is written in Python.

> John



Tue, 02 Nov 2004 13:03:59 GMT  
 Metaclasses...

Quote:
> So when you wonder, how can the type of Object be Class, if Class is an Object,

This reminds me of trying to visualize what the graphs of n > 5
dimensional functions would look like....  it's easier to not bother.

It certainly doesn't help when doing integrals or derivatives (sp) :-)

~ Patrick



Tue, 02 Nov 2004 13:20:25 GMT  
 Metaclasses...
Quote:

> In the current implementation, yes.  They need not to exist in the
> other implementaions.

Whoa! Now, I'm confused.  What's this "other" implementation you are
talking about.  I thought there was only one implementation of Ruby.
Now, it seems like there are two?  If so, why, and in what significant
ways do they differ?  Thanks.

Dennis

Quote:
>                                                    matz.



Wed, 03 Nov 2004 01:18:33 GMT  
 Metaclasses...
Hi,

In message "Re: Metaclasses..."

|> In the current implementation, yes.  They need not to exist in the
|> other implementaions.
|
|Whoa! Now, I'm confused.  What's this "other" implementation you are
|talking about.

Don't confuse.  I didn't say "They don't exist in the other
implementations".  I said "They need not to exist in the other
implementaions (if any)".

And we have JRuby the other implementation in Java anyway.  I think it
also has "meta-objects" though.

                                                        matz.



Wed, 03 Nov 2004 01:34:10 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. metaclasses are princess or: how to teach metaclasses to children ?

2. metaclasses

3. Metaclasses

4. Metaclasses

5. Examples of metaclasses as factory functions/classes?

6. Metaclasses?

7. overriding __call__ in metaclasses

8. Metaclasses & docstrings in 2.2

9. metaclasses (was Re: Hooks, aspect-oriented programming, and design by contract)

10. Metaclasses broke in 2.2?

11. Metaclasses?

12. metaclasses and setting __class__

 

 
Powered by phpBB® Forum Software