Re: A question about metaclass


#1

My question is where the C.new method is.
The class Class or C’s singleton class?

According to PickAxe2, it seems that C.new is in Class.
However, according to Little Ruby book, it seems that C.new
is in C’s metaclass.

class C
end

C.new #executed new from Class

class C
def new(arg)
p “ARGGGHHH!! #{arg}”
super.new
end
end

C.new #executed new from C’s metaclass/singleton class/eigenclass.
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################


#2

Hi –

On Thu, 24 Nov 2005, Daniel S. wrote:

C.new #executed new from Class

class C
def new(arg)

You’re defining C#new (an instance method of C). Do you mean to
define:

def C.new(arg)

?

p “ARGGGHHH!! #{arg}”
super.new

If you are defining C.new, then super will return an instance of C:

class C
def C.new
super
end
end

C.new # #<C:0x355e9c>

If you call new on that object, you’ll get an NoMethodError :slight_smile:

end
end

See if this helps:

class C
end

p C.new

def C.new
puts “Here”
super
end

p C.new

C.new #executed new from C’s metaclass/singleton class/eigenclass.

(class << obj; self; end) is obj’s singleton class. Sometimes the
singleton class of a class object is also called a “metaclass” (though
there’s been debate as to whether a separate term is needed, and
whether “metaclass” is the best separate term). “Eigenclass” is just
one of maybe 25 words that have been suggested for a future “official”
term, since Matz has said he’s thinking about what the best term might
be.

David