class.class = class

  • Thread starter Rubynewbie Rubynewbie
  • Start date
R

Rubynewbie Rubynewbie

I understand that when you define a new class it´s an instance of
Class...

class Test
end

So you can treat "Test" as an object and asks its class :

Test.class

Class


Is this Class itself an object ?

Because when i do Class.class, ruby gives me 'Class'

So is Class an instance of itself? I don´t understand the logic behind
this :(

Thanks
 
B

Bertram Scharpf

Hi,

Am Freitag, 17. Jul 2009, 20:54:26 +0900 schrieb Rubynewbie Rubynewbie:
I understand that when you define a new class it=C2=B4s an instance of
Class...
=20
class Test
end
=20
So you can treat "Test" as an object and asks its class :
=20
Test.class
=20
Class
=20
=20
Is this Class itself an object ?

Yes, it is:

Test.class.ancestors
# =3D> [Class, Module, Object, Kernel]
Class.class
# =3D> Class
Class.class.ancestors
# =3D> [Class, Module, Object, Kernel]

Bertram


--=20
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
 
J

James Coglan

So is Class an instance of itself? I don=B4t understand the logic behind


Well then you'll love this:

Class.class
#=3D> Class

Class.superclass
#=3D> Module

Class.superclass.superclass
#=3D> Object

Module.class
#=3D> Class

Object.class
#=3D> Class

Object.ancestors
#=3D> [Object, Kernel]

Kernel.class
#=3D> Module

So Object and Module are instances of a subclass of themselves, and Object
inherits from Kernel, which is a Module, a subclass of Object. Fun!

The way this actually works is that a Ruby interpreter will implement Objec=
t
using low-level building blocks, then implement Module and Class on top of
that, then perform a bunch of voodoo to make Object and Module *look* like
first-class Class instances.

Some quick rules on Ruby's type system that I find helpful:

* Object is the base type. Everything is an Object.

* Module is a type of Object that stores Methods. A module contains a list
of methods, and possibly pointers to one or more 'parent' modules mixed in
using `include`.

* Class is a type of Module that not only stores Methods but can also spawn
new Objects that have the class's methods. Additionally, classes have extra
inheritance semantics in that subclassing is more restrictive than mixins,
and class methods are inherited when subclassing.

* Kernel is an *instance* of Module that stores the core methods common to
all Objects. It is mixed into Object after Module is created, thereby addin=
g
the core methods to all extant objects in the Ruby runtime.
 
R

Rubynewbie 72

Thanks for the responses...

I´m starting to see the light at the end of the tunnel...

Still...I need to do more experimentation with the language because i
still have that "which came first, the chicken or the egg?" feeling ;)
 
E

Eric Hodel

Thanks for the responses...

I=B4m starting to see the light at the end of the tunnel...

Still...I need to do more experimentation with the language because i
still have that "which came first, the chicken or the egg?" feeling ;)

Object comes first from (object.c):

rb_cObject =3D boot_defclass("Object", 0);
rb_cModule =3D boot_defclass("Module", rb_cObject);
rb_cClass =3D boot_defclass("Class", rb_cModule);

In 1.9, it's BasicObject that comes first.=
 

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

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top