From: Josh Cheek Date: 2013-02-21T13:16:39+09:00 Subject: Re: How meta class differs from real class ? --bcaec554d5de30bf7704d6345719 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Feb 20, 2013 at 3:54 PM, Xavier R. wrote: > In Ruby- > > Could anyone help me to understand how meta class differs from real > class ? > > Examples with discussion could be better to understand the core of the > concept > > -- > Posted via http://www.ruby-forum.com/. > > As far as I know, there are only two differences: 1. It can't be instantiated Object.new.singleton_class.new # ~> -:1:in `new': can't create instance of singleton class (TypeError) # ~> from -:1:in `
' 2. It does not show up in the ancestry Superclass = Class.new # Define a method that will be in the ancestry of the subclass Superclass.define_singleton_method(:abc) { 123 } # We inherit the abc method from the singleton class of Superclass Subclass = Class.new Superclass Subclass.abc # => 123 Subclass.method(:abc).owner # => # Superclass.singleton_class # => # # But it does not show up in the ancestry Subclass.ancestors # => [Subclass, Superclass, Object, Kernel, BasicObject] Subclass.singleton_class.ancestors # => [Class, Module, Object, Kernel, BasicObject] --bcaec554d5de30bf7704d6345719 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Wed, Feb 20, 2013 at 3:54 PM, Xavier R. <lists@ruby-forum.com>= ; wrote:

As far as I know, there are only = two differences:

1. It can't be instantiated

Object.new.singleton_class.new
# ~> -:1:in `new': can't create instance of singleton class= (TypeError)
# ~> from -:1:in `<main>'
<= br>


2. It does not show up in the ance= stry

Superclass =3D Class.new
=
# Define a method that will be in the ancestry of the subcla= ss
Superclass.define_singleton_method(:abc) { 123 }

<= div># We inherit the abc method from the singleton class of Superclass
Subclass =3D Class.new Superclass
Subclass.abc =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0# =3D> 123
Subclass.method(:abc).owner # =3D> #<Class:Superclass>
<= div>Superclass.singleton_class =A0# =3D> #<Class:Superclass>
=

# But it does not show up in the ancestry
Sub= class.ancestors =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # =3D> [Subclass, Superc= lass, Object, Kernel, BasicObject]
Subclass.singleton_class.ancestors # =3D> [Class, Module, Object, K= ernel, BasicObject]

--bcaec554d5de30bf7704d6345719--