From: zdennis Date: 2006-04-13T05:44:37+09:00 Subject: Re: Difference between classes' methods and its "own" methods? -----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 iD8DBQFEPWfXMyx0fW1d8G0RAr4MAJ4v2uh4TtSuJUCbHxzx7tZWwISgRQCePEe7 ju9m6AoVzVlnldZlQPCEiFE= =aX5g -----END PGP SIGNATURE-----