Singleton classes

R

rolo

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.


regards,
rolo

"Best part of Ruby is its mailing list which Python does not have"
 
F

Florian Gross

rolo said:
Hi
Moin!

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.

I heavily assume that it's part of the language specification, because
we have this syntax:

obj = "foo"
class << obj
# everything in here is executed in the context of the singleton class
def reverse; "bogus"; end
end
obj.reverse # => "bogus"
regards,
rolo

More Regards,
Florian Gross
"Best part of Ruby is its mailing list which Python does not have"

Just one of the countless best parts of Ruby. ;)
 
M

Mauricio Fernández

Florian Gross wrote:

I heavily assume that it's part of the language specification, because
we have this syntax:

obj = "foo"
class << obj
# everything in here is executed in the context of the singleton class
def reverse; "bogus"; end
end
obj.reverse # => "bogus"

->
yes, but this does not mean we have singleton classes as such. This only
means that message reverse belongs obj. The inherent assumption is that
message processing is part of class and not object. Hence singleton class.
If the Objects themselves can process message we do not need an explicit
singleton class. This syntax shall remain even if there is no singleton
class.

It also corresponds more naturally to the following:

singleton = class << obj; self end

If there were no "real" singleton classes, you'd have to add a 'virtual
singleton' that would operate on the m_tbl of the object. Also keep in
mind that the proxy classes corresponding to singleton classes are added
lazily, i.e. there's no additional cost for an object w/o singleton
methods. That would not be the case w/ the alternate implementation
you mention.

It hence has conceptual advantages (maps more naturally to Ruby's
semantics) and obvious implementation pros. There's no point in breaking
deliberately the 'klass chain' model if it's less clear and more costly
(at least when considering the rest of the current runtime).

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

'Mounten' wird für drei Dinge benutzt: 'Aufsitzen' auf Pferde, 'einklinken'
von Festplatten in Dateisysteme, und, nun, 'besteigen' beim Sex.
-- Christa Keil
 
G

Gavin Sinclair

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.

Search the wiki (http://www.rubygarden.org/ruby) for 'singleton' and
I'm sure you'll find some interesting musings.

Cheers,
Gavin
 
D

David Alan Black

Hi --

rolo said:
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.

I know no reason to doubt that it's part of the design of the Ruby
language. Certainly if you were writing a new implementation of Ruby
it would have to run code that used singleton classes. If your
implementation didn't use Class objects, but somehow masqueraded
something else as Class objects, I guess that would be OK, as long as
they gave the precise appearance and behavior of Class objects on the
Ruby side.


David
 
T

ts

r> def A.classMethod
r> end

r> def a.objectMethod
r> end

Why do you make a distinction between classMethod and objectMethod ?

A class is an object.


Guy Decoux
 
R

Robert Klemme

rolo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,373
Latest member
Desiree036

Latest Threads

Top