<ppdoc>
<copyright>
Copyright (c) 2001 by Addison Wesley Longman, Inc. This material may
be distributed only subject to the terms and conditions set forth in
the Open Publication License, v1.0 or later (the latest version is
presently available at http://www.opencontent.org/openpub/).
<p/>
Distribution of substantively modified versions of this document is
prohibited without the explicit permission of the copyright holder.
<p/>
Distribution of the work or derivative of the work in any standard
(paper) book form is prohibited unless prior permission is obtained
from the copyright holder.
</copyright>
<class name="Class" super="Module" type="class" ref="xref-000001">
<p/>
  Classes in Ruby are first-class objects---each is an instance of
  class <classname>Class</classname>.
<p/>
  When a new class is created (typically using
  <tt>class<nbsp/>Name<nbsp/>...<nbsp/>end</tt>), an object of type <classname>Class</classname> is created
  and assigned to a global constant (<tt>Name</tt> in this case). When
  <tt>Name.new</tt> is called to create a new object, the <meth>new</meth>
  method in <classname>Class</classname> is run by default. This can be demonstrated by
  overriding <meth>new</meth> in <classname>Class</classname>:
<p/>
<codefragment>
<alltt><fullcode><![CDATA[    class Class
       alias oldNew  new
       def new(*args)
         print "Creating a new ", self.name, "\n"
         oldNew(*args)
       end
     end

     class Name
     end

     n = Name.new
]]></fullcode>
class<nbsp/>Class
<nbsp/><nbsp/><nbsp/>alias<nbsp/>oldNew<nbsp/><nbsp/>new
<nbsp/><nbsp/><nbsp/>def<nbsp/>new(*args)
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>print<nbsp/>"Creating<nbsp/>a<nbsp/>new<nbsp/>",<nbsp/>self.name,<nbsp/>"\n"
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>oldNew(*args)
<nbsp/><nbsp/><nbsp/>end
<nbsp/>end
<p/>
<nbsp/>class<nbsp/>Name
<nbsp/>end
<p/>
<nbsp/>n<nbsp/>=<nbsp/>Name.new
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
Creating<nbsp/>a<nbsp/>new<nbsp/>Name
</alltt>
</codefragment>
<p/>
  <methods type="class">
        <method name="inherited" ref="inherited">
      <callseq>
        <obj>aClass</obj>.inherited( <obj>aSubClass</obj> )
      </callseq>
      <desc>
<p/>
      This is a singleton method (per class) invoked by Ruby when a subclass
      of <obj>aClass</obj> is created. The new subclass is passed as a
      parameter.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[        class Top
          def Top.inherited(sub)
            print "New subclass: ", sub, "\n"
          end
        end

        class Middle < Top
        end

        class Bottom < Middle
        end
]]></fullcode>
class<nbsp/>Top
<nbsp/><nbsp/>def<nbsp/>Top.inherited(sub)
<nbsp/><nbsp/><nbsp/><nbsp/>print<nbsp/>"New<nbsp/>subclass:<nbsp/>",<nbsp/>sub,<nbsp/>"\n"
<nbsp/><nbsp/>end
end
<p/>
class<nbsp/>Middle<nbsp/>&lt;<nbsp/>Top
end
<p/>
class<nbsp/>Bottom<nbsp/>&lt;<nbsp/>Middle
end
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
New<nbsp/>subclass:<nbsp/>Middle
New<nbsp/>subclass:<nbsp/>Bottom
</alltt>
</codefragment>
<p/>
      </desc>
    </method>
<p/>
        <method name="new" ref="new">
      <callseq>
        Class.new( <obj>aSuperClass</obj>=<classname>Object</classname> )
        <returns><obj>aClass</obj></returns>
      </callseq>
      <desc>
<p/>
      Creates a new anonymous (unnamed)            
      class with the given superclass (or
      <classname>Object</classname> if no parameter is given).
<p/>
      </desc>
    </method>
<p/>
  </methods>
<p/>
  <methods type="instance">
<p/>
        <method name="new" ref="new">
      <callseq>
        <obj>aClass</obj>.new( <optz><obj>args</obj></optz> )
        <returns><obj>anObject</obj></returns>
      </callseq>
      <desc>
<p/>
      Creates a new object of <obj>aClass</obj>'s class, then invokes that object's
      <meth>initialize</meth> method, passing it <obj>args</obj>.
<p/>
      </desc>
    </method>
<p/>
        <method name="superclass" ref="superclass">
      <callseq>
        <obj>aClass</obj>.superclass
        <returns><obj>aSuperClass</obj> or <tt>nil</tt></returns>
      </callseq>
      <desc>
<p/>
      Returns the superclass of <obj>aClass</obj>, or <const>nil</const>.
<p/>
<codefragment>
<fullcode><![CDATA[      Class.superclass
      Object.superclass
]]></fullcode><rubycode>
<tr>
  <td><tt>Class.superclass</tt></td>
  <td>&#187;</td>
  <td><tt>Module</tt></td>
</tr>
<tr>
  <td><tt>Object.superclass</tt></td>
  <td>&#187;</td>
  <td><tt>nil</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
      </desc>
    </method>
<p/>
  </methods>
<p/>
</class>
</ppdoc>
