From: Matt Mongeau Date: 2013-02-21T07:19:55+09:00 Subject: Re: How meta class differs from real class ? --e89a8f6429a4b2feb004d62f4161 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable I think I best understood this problem when trying to write my own programming languages. Here's the basic case that you actually end up using (albeit indirectly) almost all of the time in Ruby. Everything in ruby is an object, objects respond to methods. This means that an object must know about the methods it responds to. So lets take a class for an example class MyClass def my_instance_method; end def self.my_class_method; end end So in the c land we have some class which needs to hold both of these methods, but here's what is interesting, objects only hold their instance methods. This means in c land the MyClass object only knows about it's my_instance_method. Well than, what knows about the class method? We can't put it's my_class_method on it's superclass because that would add my_class_method to Class. This presents a problem. We solve this by adding a class in between MyClass and Class. This goes by many names, eigenclass, metaclass, or in the actual ruby implementation the singleton_class. Here's an example showing how this method is attached: =E6=B0=B4~/Code/ruby/with_c=E2=80=B91.9.3-p374=E2=80=BA$ irb irb(main):001:0> class MyClass irb(main):002:1> def my_instance_method irb(main):003:2> end irb(main):004:1> def self.my_class_method irb(main):005:2> end irb(main):006:1> end =3D> nil irb(main):007:0> MyClass.singleton_class =3D> # irb(main):008:0> MyClass =3D> MyClass irb(main):009:0> MyClass.new.method(:my_instance_method).owner =3D> MyClass irb(main):010:0> MyClass.method(:my_class_method).owner =3D> # In the above example you can see the my_class_method is attached to the singleton_class. To be honest this is a pretty elegant solution to this problem. --e89a8f6429a4b2feb004d62f4161 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I think I best understood this problem when trying to writ= e my own programming languages. Here's the basic case that you actually= end up using (albeit indirectly) almost all of the time in Ruby.

Everything in ruby is an object, objects respond to methods. Thi= s means that an object must know about the methods it responds to. So lets = take a class for an example

class MyClass
=C2=A0 def my_instance_method; end
=C2=A0 def se= lf.my_class_method; end
end

So in the c land we have some class which needs to hold both of these= methods, but here's what is interesting, objects only hold their insta= nce methods. This means in c land the MyClass object only knows about it= 9;s my_instance_method.

Well than, what knows about the class metho= d? We can't put it's my_class_method on it's superclass because= that would add my_class_method to Class. This presents a problem. We solve= this by adding a class in between MyClass and Class.

This goes by many names, eigenclass, metacl= ass, or in the actual ruby implementation the singleton_class. Here's a= n example showing how this method is attached:

=E6=B0=B4~/Code/ruby/with_c=E2=80=B91.9.3-p374=E2=80=BA$ ir= b
irb(main):001:0> class MyClass
irb(main):002:1>= def my_instance_method
irb(main):003:2> end
irb(mai= n):004:1> def self.my_class_method
irb(main):005:2> end
irb(main):006:1> end
= =3D> nil
irb(main):007:0> MyClass.singleton_class
=3D> #<Class:MyClass>
irb(main):008:0> MyClass
=
=3D> MyClass
irb(main):009:0> MyClass.new.method(:my_instan= ce_method).owner
=3D> MyClass
irb(main):010:0> My= Class.method(:my_class_method).owner
=3D> #<Class:MyClass&g= t;

In the above example you can see the my_class_met= hod is attached to the singleton_class. To be honest this is a pretty elega= nt solution to this problem.

--e89a8f6429a4b2feb004d62f4161--