# Difference between classes' methods and its "own" methods?

**URL:** <https://www.ruby-forum.com/t/difference-between-classes-methods-and-its-own-methods/93876>\
**Category:** Ruby\
**Created:** [April 12, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/difference-between-classes-methods-and-its-own-methods/93876 "2006-04-12T21:15:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 12, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/difference-between-classes-methods-and-its-own-methods/93876/1 "2006-04-12T21:15:34Z")

</div>

Little confused, why don’t the product the same thing?

X.methods(false)  
=\> [“xxx”]

irb(main):018:0\> (class \<\< X; self; end).instance\_methods(false)  
=\> [“xxx”, “to\_yaml”, “yaml\_tag\_subclasses?”, “new”, “superclass”,  
“allocate”]

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 12, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/difference-between-classes-methods-and-its-own-methods/93876/2 "2006-04-12T22:47:08Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

TRANS wrote:

> Little confused, why don’t the product the same thing?
> 
> X.methods(false)  
> =\> [“xxx”]
> 
> irb(main):018:0\> (class \<\< X; self; end).instance\_methods(false)  
> =\> [“xxx”, “to\_yaml”, “yaml\_tag\_subclasses?”, “new”, “superclass”, “allocate”]

I may be wrong, but I think…it has to do with the ‘virtual class  
instance X of Class’. ie: (class \<\< X; self; end)

The virtual class instance itself is a clone of it’s superclass. In this  
case, the superclass of X is Class]. Since the virtual  
class instance is a clone of Class it includes all of it’s  
singleton\_methods, plus it’s own which are defined after the call to  
“class X”

This works as you go up the inheritance chain with more subclasses:

irb(main):001:0\> class X; def self.xxx; end; end  
=\> nil  
irb(main):002:0\> X.methods false  
=\> [“xxx”]  
irb(main):003:0\> (class \<\< X ; self ; end ).instance\_methods false  
=\> [“xxx”, “new”, “superclass”, “allocate”]  
irb(main):004:0\> class Y \< X; end;  
irb(main):005:0\* Y.methods false  
=\> []  
irb(main):006:0\> (class \<\< Y ; self ; end ).instance\_methods false  
=\> [“xxx”, “new”, “superclass”, “allocate”]  
irb(main):007:0\> class Z \< Y; end;  
irb(main):008:0\* (class \<\< Z ; self ; end ).instance\_methods false  
=\> [“xxx”, “new”, “superclass”, “allocate”]

The superclass is getting _cloned_ during the creation of the subclass.  
I think that clone is being used as the virtual class  
instance.

I may be way off, but I thought I’d attempt to make sense. I am going to  
play with this tonight or tomorrow… class.c is my target…

Zach  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.1 (GNU/Linux)  
Comment: Using GnuPG with Thunderbird - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFEPWfXMyx0fW1d8G0RAr4MAJ4v2uh4TtSuJUCbHxzx7tZWwISgRQCePEe7  
ju9m6AoVzVlnldZlQPCEiFE=  
=aX5g  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 13, 2006, 6:27am UTC](https://www.ruby-forum.com/t/difference-between-classes-methods-and-its-own-methods/93876/3 "2006-04-13T06:27:10Z")

</div>

zdennis wrote:

> > irb(main):018:0\> (class \<\< X; self; end).instance\_methods(false)  
> > This works as you go up the inheritance chain with more subclasses:  
> > irb(main):006:0\> (class \<\< Y ; self ; end ).instance\_methods false  
> > Zach  
> > irb(main):001:0\> (class \<\< Class; self; end).object\_id  
> > =\> 537826910  
> > irb(main):002:0\> class A; def self.foo; end; end  
> > =\> nil  
> > irb(main):003:0\> class B \< A; end  
> > =\> nil  
> > irb(main):004:0\> class C \< B; end  
> > =\> nil  
> > irb(main):005:0\> (class \<\< A; self; end).superclass.object\_id  
> > =\> 537826910  
> > irb(main):006:0\> (class \<\< B; self; end).superclass.object\_id  
> > =\> 537826910  
> > irb(main):007:0\> (class \<\< C; self; end).superclass.object\_id  
> > =\> 537826910

It appears that the virtual class of Class is being used as the  
superclass of the virtual classes of other classses.

I have not myself looked into class.c, and am all ears for what Zach  
would find.

Best regards,

JS
