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?
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?