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
|
<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="Method" super="Object" type="class">
Method objects are created by <cim><file>object</file><front>Object</front><back>method</back><mref>method</mref></cim>, and are associated
with a particular object (not just with a class). They may be used
to invoke the method within the object, and as a block associated with
an iterator.
<p/>
<codefragment>
<fullcode><![CDATA[ class Thing
def square(n)
n*n
end
end
aThing = Thing.new
aMethod = aThing.method("square")
aMethod.call(9)
[ 1, 2, 3 ].collect(&aMethod)
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>class<nbsp/>Thing</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>def<nbsp/>square(n)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>n*n</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt>aThing<nbsp/><nbsp/>=<nbsp/>Thing.new</tt></td>
</tr>
<tr>
<td colspan="3"><tt>aMethod<nbsp/>=<nbsp/>aThing.method("square")</tt></td>
</tr>
<tr>
<td colspan="3"><tt></tt></td>
</tr>
<tr>
<td><tt>aMethod.call(9)</tt></td>
<td>»</td>
<td><tt>81</tt></td>
</tr>
<tr>
<td><tt>[<nbsp/>1,<nbsp/>2,<nbsp/>3<nbsp/>].collect(&aMethod)</tt></td>
<td>»</td>
<td><tt>[1,<nbsp/>4,<nbsp/>9]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
<methods type="instance">
<method name="[ ]" ref="_ob_cb">
<callseq>
<obj>meth</obj>[ <optz><obj>args</obj></optz> ]
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Synonym for <tt>Method.call</tt>.
<p/>
</desc>
</method>
<p/>
<method name="arity" ref="arity">
<callseq>
<obj>meth</obj>.arity <returns><obj>aFixnum</obj></returns>
</callseq>
<desc>
<p/>
Returns an indication of the number of arguments accepted by a method.
Returns a nonnegative integer for methods that take a fixed number
of arguments. For Ruby methods that take a variable number of
arguments, returns -n-1, where n is the number of required
arguments. For methods written in C, returns -1 if the call
takes a variable number of arguments.
<p/>
</desc>
</method>
<p/>
<method name="call" ref="call">
<callseq>
<obj>meth</obj>.call( <optz><obj>args</obj></optz> )
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Invokes the <obj>meth</obj> with the specified
arguments, returning the method's return value.
<p/>
<codefragment>
<fullcode><![CDATA[ m = 12.method("+")
m.call(3)
m.call(20)
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>m<nbsp/>=<nbsp/>12.method("+")</tt></td>
</tr>
<tr>
<td><tt>m.call(3)</tt></td>
<td>»</td>
<td><tt>15</tt></td>
</tr>
<tr>
<td><tt>m.call(20)</tt></td>
<td>»</td>
<td><tt>32</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="to_proc" ref="to_proc">
<callseq>
<obj>meth</obj>.to_proc <returns><obj>aProc</obj></returns>
</callseq>
<desc>
<p/>
Returns a <classname>Proc</classname> object corresponding to this method.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
</class>
</ppdoc>
|