Singleton classes

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”

rolo wrote:

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. :wink:

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

Cheers,
Gavin

···

On Monday, May 17, 2004, 4:05:07 AM, rolo wrote:

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.

Hi –

“rolo” rohitlodha@hotwireindia.com writes:

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

···


David A. Black
dblack@wobblini.net

  def A.classMethod
  end

def a.objectMethod
end

Why do you make a distinction between classMethod and objectMethod ?

A class is an object.

Guy Decoux

“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

rolo wrote:

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.

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.

rdgs,
rolo

···

-----Original Message-----
From: Florian Gross [mailto:flgr@ccan.de]
Sent: Sunday, May 16, 2004 11:54 PM
To: ruby-talk ML
Subject: Re: Singleton classes

Why do you make a distinction between classMethod and objectMethod ?

A class is an object.

Agreed. just a way to refer them in example.

rolo

···

-----Original Message-----
From: ts [mailto:decoux@moulon.inra.fr]
Sent: Monday, May 17, 2004 2:57 PM
To: ruby-talk ML
Cc: ruby-talk@ruby-lang.org
Subject: Re: Singleton classes

def A.classMethod
end

def a.objectMethod
end

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

···

On Mon, May 17, 2004 at 06:35:59AM +0900, rolo wrote:

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.


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