Forum: Ruby Difference between classes' methods and its "own" methods?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
TRANS (Guest)
on 2006-04-12 23:15
(Received via mailing list)
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.
zdennis (Guest)
on 2006-04-13 00:47
(Received via mailing list)
-----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-----
Srinivas J. (Guest)
on 2006-04-13 08:27
(Received via mailing list)
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
This topic is locked and can not be replied to.