A question about Class and Object

All other classes are an instance of Class (Object is an instance of

Class).

What about Class?
It's an instance of itself, right?

"Class" is an object, whose class is Class. :slight_smile:

To test this strange thing, I tried to subclass Class but it failed.
class MyClass < Class
end
=>TypeError: can't make subclass of Class

That is because you're trying to extend the concept of Class-ness, not
of a particular class. That will be too hard to maintain, so Ruby
forbids it.

Now, I created an instance of Class.
aClass = Class.new
Is aClass a class like Object or just an object like obj (obj =

Object.new)?

It seems like a class.

You will see that: aClass.class gives Class, so aClass is an object
whose class is Class.

aaClass = aClass.new #->Works!

Now aaClass is an object, whose class is aClass. You will not see this
in irb because you have not named aClass. Naming is done by binding it
to a constant (beginning with a capital):

irb(main):001:0> Fred = Class.new
=> Fred
irb(main):002:0> aFred = Fred.new
=> #<Fred:0x2b436e0>
irb(main):003:0> Fred.class
=> Class
irb(main):004:0> aFred.class
=> Fred

So Class is actually a class maker.
It's instances are classes.
That means that to make a class, I don't have to define a class.
If I just create an instance of Class, it's a class.

Correct. For another example, Struct#new is also a "class maker":

irb(main):005:0> struct = Struct.new "MyStructClass", :foo, :bar
=> Struct::MyStructClass
irb(main):006:0> struct.class
=> Class
irb(main):007:0> st1 = struct.new
=> #<struct Struct::MyStructClass foo=nil, bar=nil>
irb(main):009:0> st1.foo = 1
=> 1

We have added a new class to the runtime, called MyStructClass.

When I think of Class and Object, the "chicken and egg" problem comes

to my

mind.
Object is an instance of Class and Class inherits from Object.
Which one is first? (Which one should exist first?)

irb(main):010:0> Object.id
=> 20796620
irb(main):011:0> Class.id
=> 20796596

So it seems that the Class class-object is created before the Object
class-object :slight_smile:

Try these (make sure you understand each one before going to the next):

* Every object has a singleton-class associated with it.
* The singleton-class is unique for that object and holds all the
  methods descriptions etc for that object.
* All objects are created from a boiler-plate class. Until they are
  modified the singleton-classes for them will be identical to the
  class-object they were created from.
* Concepts are represented as classes.
* Instances of concepts are represented as objects.
* "Object" is an identifier that represents the concept for "thing".
Being
  a concept its singelton-class is Class.
* "Class" is the identifier to represent the concept of "class", or the
actual
  representation of the concept of concepts. Therefore its
singelton-class is
  also Class.
* Singleton-classes in themselves are concepts. Therefore they are
represented as
  _objects_ of class _Class_.
* Inheritance is used to signify a lot of things (specification,
extension etc).
  In practice it means take the specification of the parent and
  modify it.
* The minimal information you have of something is that it is a "thing".
  All "things" have a low common denominator.
  Therefore everything inherits from Object.
* Class is a more specific concept than "thing".
  Therefore "Class" inherits from Object.
* Object is a concept. Therefore it is of the class of things that are
concepts.
  Therefore Object's class is Class.
* The order of the creation of the objects is the Ruby (or any other
runtime for
  that matter) bootstrap process. It is as immaterial a question as
"what
  happened before the big bang": There is a magic that creates the
memory
  allocations before you have access to the runtime.

HTH. If not try books by Wittgenstein, Godel and Chomsky.

Who are you?
I would have thought that you were "matz" if I hadn't seen your name.

No, no...
Don't expect me to fully understand what you said.
I'm just a beginner.

I think what bothers me is terminologies.
Class is an object and Object is class.
It's pretty recursive.
It would be better if we have different terms for each.

Thank you very much.

Sam
"Mehr, Assaph (Assaph)" <assaph@avaya.com> wrote in message
news:338366A6D2E2CA4C9DAEAE652E12A1DE166E1C@au3010avexu1.global.avaya.com...

路路路

> All other classes are an instance of Class (Object is an instance of
Class).
> What about Class?
> It's an instance of itself, right?

"Class" is an object, whose class is Class. :slight_smile:

> To test this strange thing, I tried to subclass Class but it failed.
> class MyClass < Class
> end
> =>TypeError: can't make subclass of Class

That is because you're trying to extend the concept of Class-ness, not
of a particular class. That will be too hard to maintain, so Ruby
forbids it.

> Now, I created an instance of Class.
> aClass = Class.new
> Is aClass a class like Object or just an object like obj (obj =
Object.new)?
> It seems like a class.

You will see that: aClass.class gives Class, so aClass is an object
whose class is Class.

> aaClass = aClass.new #->Works!

Now aaClass is an object, whose class is aClass. You will not see this
in irb because you have not named aClass. Naming is done by binding it
to a constant (beginning with a capital):

irb(main):001:0> Fred = Class.new
=> Fred
irb(main):002:0> aFred = Fred.new
=> #<Fred:0x2b436e0>
irb(main):003:0> Fred.class
=> Class
irb(main):004:0> aFred.class
=> Fred

> So Class is actually a class maker.
> It's instances are classes.
> That means that to make a class, I don't have to define a class.
> If I just create an instance of Class, it's a class.

Correct. For another example, Struct#new is also a "class maker":

irb(main):005:0> struct = Struct.new "MyStructClass", :foo, :bar
=> Struct::MyStructClass
irb(main):006:0> struct.class
=> Class
irb(main):007:0> st1 = struct.new
=> #<struct Struct::MyStructClass foo=nil, bar=nil>
irb(main):009:0> st1.foo = 1
=> 1

We have added a new class to the runtime, called MyStructClass.

> When I think of Class and Object, the "chicken and egg" problem comes
to my
> mind.
> Object is an instance of Class and Class inherits from Object.
> Which one is first? (Which one should exist first?)

irb(main):010:0> Object.id
=> 20796620
irb(main):011:0> Class.id
=> 20796596

So it seems that the Class class-object is created before the Object
class-object :slight_smile:

Try these (make sure you understand each one before going to the next):

* Every object has a singleton-class associated with it.
* The singleton-class is unique for that object and holds all the
  methods descriptions etc for that object.
* All objects are created from a boiler-plate class. Until they are
  modified the singleton-classes for them will be identical to the
  class-object they were created from.
* Concepts are represented as classes.
* Instances of concepts are represented as objects.
* "Object" is an identifier that represents the concept for "thing".
Being
  a concept its singelton-class is Class.
* "Class" is the identifier to represent the concept of "class", or the
actual
  representation of the concept of concepts. Therefore its
singelton-class is
  also Class.
* Singleton-classes in themselves are concepts. Therefore they are
represented as
  _objects_ of class _Class_.
* Inheritance is used to signify a lot of things (specification,
extension etc).
  In practice it means take the specification of the parent and
  modify it.
* The minimal information you have of something is that it is a "thing".
  All "things" have a low common denominator.
  Therefore everything inherits from Object.
* Class is a more specific concept than "thing".
  Therefore "Class" inherits from Object.
* Object is a concept. Therefore it is of the class of things that are
concepts.
  Therefore Object's class is Class.
* The order of the creation of the objects is the Ruby (or any other
runtime for
  that matter) bootstrap process. It is as immaterial a question as
"what
  happened before the big bang": There is a magic that creates the
memory
  allocations before you have access to the runtime.

HTH. If not try books by Wittgenstein, Godel and Chomsky.

[...]

HTH. If not try books by Wittgenstein, Godel and Chomsky.

I'm glad you mentioned Godel; I was going to make a Godelian remark
but restrained myself.

It reminds me of the efforts of Russel and Whitehead to formulate set
theory in a way which was complete while also being consistent -- for
example being without self-reference paradoxes. Can a set contain
itself? To avoid self-reference, they constructed an elaborate
hierarchy of objects, meta-objects, meta-meta-objects, and so fourth.
Objects in one level were not allowed to make reference to objects in
the level above.

It all crumbled in 1931 when Godel published his paper, proving that
such a system was impossible -- you cannot be complete and consistent
at the same time.

The book "Godel, Escher, Bach" should be really required reading for
every individual. Just the first thirty pages are enough to alter
your brain patterns forever :slight_smile: Strange Loops exist in the very heart
of language and logic. I like to think of Object.class == Class.class
== Class as a kind of Strange Loop.

路路路

--- "Mehr, Assaph (Assaph)" <assaph@avaya.com> wrote:

__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail

irb(main):010:0> Object.id
=> 20796620
irb(main):011:0> Class.id
=> 20796596

So it seems that the Class class-object is created before the Object
class-object :slight_smile:

or after :slight_smile:

svg% ruby -e 'p Object.id,Class.id,Array.id'
537220752
537220732
537205612
svg%

Guy Decoux

All other classes are an instance of Class (Object is an instance of

Class).

What about Class?
It's an instance of itself, right?

"Class" is an object, whose class is Class. :slight_smile:

To test this strange thing, I tried to subclass Class but it failed.
class MyClass < Class
end
=>TypeError: can't make subclass of Class

That is because you're trying to extend the concept of Class-ness, not
of a particular class. That will be too hard to maintain, so Ruby
forbids it.

actually, does really ruby forbids it?

class My< Class
end

TypeError: can't make subclass of Class
        from (irb):1

My=Class.new(Class)

=> My

My.superclass

=> Class

My.new

TypeError: wrong instance allocation
        from (irb):7:in `new'
        from (irb):7

strange behaviour :slight_smile:

路路路

il Wed, 23 Jun 2004 12:39:34 +0900, "Mehr, Assaph (Assaph)" <assaph@avaya.com> ha scritto::

Okay, try this. *Everything* in ruby is an object. There are certain
kinds of objects that are templates for creating other objects, we call
those classes. These classes form an inheritance hierarchy, at the base
of which is a common template for creating any object, and this is the
Object class.

Now, since these templates are themselves objects, they need to be
created via a template too. This template is the Class class. It is a
class, since it is a template for creating objects (template objects in
this case), and it is an object since the templates themselves are just
objects. And conceptually it *is* recursive, since Class is both a
template for creating class objects and a class object itself, but
that's not a problem at the language level (it needs some bootstrapping
at the *implementation* level - see
http://www.ruby-talk.org/blade/104332 for an excellent description of
this).

martin

路路路

Sam Sungshik Kong <ssk@chol.nospam.net> wrote:

I think what bothers me is terminologies.
Class is an object and Object is class.
It's pretty recursive.
It would be better if we have different terms for each.

It would be better if we have different terms for each.

gabriele renzi wrote:

That is because you're trying to extend the concept of Class-ness, not
of a particular class. That will be too hard to maintain, so Ruby
forbids it.

actually, does really ruby forbids it?

class My< Class
end

TypeError: can't make subclass of Class
        from (irb):1

My=Class.new(Class)

=> My

My.superclass

=> Class

My.new

TypeError: wrong instance allocation
        from (irb):7:in `new'
        from (irb):7

strange behaviour :slight_smile:

I think strange behavior is to be expected when you do
strange things. Just my opinion.

This reminds me of my D&D days, when someone put a Portable Hole
inside a Bag of Holding...

Hal

I think strange behavior is to be expected when you do
strange things. Just my opinion.

that's reasonable but IMO one of
* allowing Class'subclass based hierarchy
* Class.new(Class) to raise an exception

would be better

This reminds me of my D&D days, when someone put a Portable Hole
inside a Bag of Holding...

:))

路路路

il Wed, 23 Jun 2004 19:57:03 +0900, Hal Fulton <hal9000@hypermetrics.com> ha scritto::

"gabriele renzi" <surrender_it@remove.yahoo.it> schrieb im Newsbeitrag
news:nmqid090gp4sdq7a1021t7d69dhnv2uho7@4ax.com...

路路路

il Wed, 23 Jun 2004 19:57:03 +0900, Hal Fulton > <hal9000@hypermetrics.com> ha scritto::

>I think strange behavior is to be expected when you do
>strange things. Just my opinion.

that's reasonable but IMO one of
* allowing Class'subclass based hierarchy
* Class.new(Class) to raise an exception

would be better
>This reminds me of my D&D days, when someone put a Portable Hole
>inside a Bag of Holding...

=> Beatles: "Yellow Submarine" - the movie. :-))

    robert