1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
<ppdoc>
<copyright>
Copyright (c) 2001 by Addison Wesley Longman. 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/).
</copyright>
<class name="Class" super="Module" type="class">
<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/><<nbsp/>Top
end
<p/>
class<nbsp/>Bottom<nbsp/><<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>»</td>
<td><tt>Module</tt></td>
</tr>
<tr>
<td><tt>Object.superclass</tt></td>
<td>»</td>
<td><tt>nil</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
</class>
</ppdoc>
|