Forum: Ruby Re: A question about metaclass

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
daniels (Guest)
on 2005-11-24 04:19
(Received via mailing list)
> 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.
#####################################################################################
dblack (Guest)
on 2005-11-24 04:56
(Received via mailing list)
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 :-)

>  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
This topic is locked and can not be replied to.