"rolo" <rohitlodha / hotwireindia.com> schrieb im Newsbeitrag
news:ACEPLIKLBKEMDDIELPKIEEKACEAA.rohitlodha / hotwireindia.com...
> Hi
>
> class A
> def instanceMethod
> end
> def A.classMethod
> end
> end
>
> a = A.new
>
> def a.objectMethod
> end
>
> Ruby uses Singleton class for the object a that contains the message
table
> with objectMethod in it. Similarly for classMethod.
>
> Is use of singleton class a specification of ruby or it is
implementation
> choice? for example, we can define internal structure for objects in
such a
> way that it contains message table.

Two options:

1) store all methods in instance's method tables.  That would be highly
memory inefficient because in a typical application most objects don't
have per instance methods and thus could easily share their class's method
table.

2) Store only singleton methods in an instance's method table.  That would
add unnecessary complexity to the interpreter implementation, because all
the lookup code would have to be changed whereas the full logic is already
there for class instances (i.e. delegating to the next higher class if not
found in this class etc.).

Apart from that, is an instance's class the place to hold this kind of
information.  For example, when cloning an instance you would have to copy
all those instance method references, too.

Kind regards

    robert