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

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.

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

zdennis wrote:

-----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

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