so lost on singleton, metaclass and virtual class

D

Dorren

Q1: in pick axe book page 364, it talks about metaclass should be a
singleton class. but when I run the following code, class's object_id
are the same for all instance, but the metaclass object_id are
different for each object instance, is the code wrong or what?

# === begin code

# snippet from http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
class Object
# The hidden singleton lurks behind everyone
def metaclass; class << self; self; end; end

def show_meta
puts "--------- #{to_s} ------------"
puts "oid: #{object_id} #{self.class.name}"
puts "class.oid: #{self.class.object_id}
#{self.class.class.name}"
puts "metaclass.oid: #{metaclass.object_id}
#{self.class.metaclass.class.name}"

o = Object.new # show methods
meta = metaclass
puts "object methods: #{(public_methods -
o.public_methods).sort.inspect}"
puts " class methods: #{(self.class.public_methods -
o.class.public_methods).sort.inspect}"
puts " meta methods: #{(meta.public_methods -
Class.public_methods).sort.inspect}"
puts
end
end

# ------- fixtures for our test --------------
class A
def initialize(v); @v = v; end
def to_s; @v; end

def m1; "method1 " end
def self.class_m0; "class method0"; end
end

b = A.new("b")
c = A.new("c")
b.show_meta
c.show_meta

# === end code


Q2: from pick axe book 364-367, it used singleton, metaclass, and
virtual class definition interchangeably. are they all the same thing
or not?

Q3: from page 365,
"To handle per-object attributes, Ruby provides a classlike some-
thing for each object that is sometimes called a singleton class."
if runtime create something new for EACH object, how can it be called
singleton?
 
D

Dorren

google group totally screwed up the code, here it is again:
--------------------------------------------------------------

# http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
class Object
# The hidden singleton lurks behind everyone
def metaclass; class << self; self; end; end

def show_meta
puts "--------- #{to_s} ------------"
puts "oid: " +
"#{object_id} #{self.class.name}"
puts "class.oid: #{self.class.object_id} " +
"#{self.class.class.name}"
puts "metaclass.oid: #{metaclass.object_id} " +
"#{self.class.metaclass.class.name}"

o = Object.new # show methods
meta = metaclass
puts "object methods: " +
"#{(public_methods - o.public_methods).sort.inspect}"
puts " class methods: " +
"#{(self.class.public_methods -
o.class.public_methods).sort.inspect}"
puts " meta methods: " +
"#{(meta.public_methods - Class.public_methods).sort.inspect}"
puts
end
end

# ------- fixtures for our test --------------
class A
def initialize(v); @v = v; end
def to_s; @v; end

def m1; "method1 " end
def self.class_m0; "class method0"; end
end

b = A.new("b")
c = A.new("c")
b.show_meta
c.show_meta
 
R

Robert Klemme

Q1: in pick axe book page 364, it talks about metaclass should be a
singleton class. but when I run the following code, class's object_id
are the same for all instance, but the metaclass object_id are
different for each object instance, is the code wrong or what?

The term "singleton class" has been discussed a lot. Other names have
been suggested but I guess "singleton class" sticks because the book
uses it. :)

Basically it just means that there is one singleton class per instance.
This class is invisible most of the time - you basically see it only
when you do class<<self. You can imagine that this class sits between
the instance and the original class (the return value of self.class).

Q2: from pick axe book 364-367, it used singleton, metaclass, and
virtual class definition interchangeably. are they all the same thing
or not?

Um, I would have to check with my copy which I don't have here with me.
But it is very possible.
Q3: from page 365,
"To handle per-object attributes, Ruby provides a classlike some-
thing for each object that is sometimes called a singleton class."
if runtime create something new for EACH object, how can it be called
singleton?

See my explanation above.

Kind regards

robert
 
R

Rick DeNatale

Q1: in pick axe book page 364, it talks about metaclass should be a
singleton class. but when I run the following code, class's object_id
are the same for all instance, but the metaclass object_id are
different for each object instance, is the code wrong or what?

Here we are talking about singleton classes used to provide instance
specific behavior.

Singleton here means that the singleton class has only one instance.
So each instance which needs one, has its own singleton class.
Q2: from pick axe book 364-367, it used singleton, metaclass, and
virtual class definition interchangeably. are they all the same thing
or not?

No they arent.

Singleton classes are classes which are marked internally with a flag
bit called FL_SINGLETON. You don't normally see these classes unless
you use the class <<self;self;end trick.

Metaclass isn't really a Ruby term, it's a term from Smalltalk in
which the classes of class objects were instances of a class named
Metaclass. Although Ruby class object do indeed have classes, they ar
not explicit metaclasses. Instead the Ruby implementation uses
classes marked as singletons for this purpose. In the pickaxe, Matz is
quoted as saying that instead of metaclasses, Ruby has singleton
classes of classes which "behave just like Smalltalk's metaclasses."

Why do class objects need classes? Classes provide a place to store a
hash of method names to methods for their instances. So when a method
of a particular object is invoked, or equivalently as us old
Smalltalkers like to say a message is sent to an object, the search
for the corresponding method starts with the objects class then up a
chain of that class' superclass etc.

Virtual classes don't appear in the current ruby implementation, maybe
they did when the pickaxe was written, maybe not. I think that the
most interesting thing Matz has to say in the Pickaxe is:

"IN THE CURRENT IMPLEMENTATION (emphasis mine) singleton classes are
specially flagged class objects between objects and their class.
These can be called "virtual" classes if the language implementer
chooses."

If you look at the current (1.8.5-1.9) implementation there are two
kinds of class-like objects which are "between objects and their
class." The first are classes marked with FL_SINGLETON which I've been
talking about, the second are internal objects of type T_ICLASS which
represent modules included by a Module/Class or extended by an object.
So I interpret the pickaxe term 'virtual class' to mean either a
singleton class or one of these T_ICLASS module proxies.
Q3: from page 365,
"To handle per-object attributes, Ruby provides a classlike some-
thing for each object that is sometimes called a singleton class."
if runtime create something new for EACH object, how can it be called
singleton?

This is really the same question as Q1.

This relates to an earlier thread here from last week. I just
converted my reply to that thread to a blog entry at:
http://talklikeaduck.denhaven2.com/articles/2007/06/02/chain-chain-chain

Which might make some of this more understandable.

HTH
 

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
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top