📄 2.7.htm
字号:
name=IXT-2-127019></A><A name=IXT-2-127020></A>A block
introduces its own scope for new local variables. The local
variables that appear first in the block are local to that
block. The scope introduced by a block can refer local
variables of outer scope; on the other hand, the scope
introduced by <TT>class</TT>, <TT>module</TT> and <TT>def</TT>
statement can't refer outer local variables. </P>
<P class=docText>The form <TT>{...}</TT> has a higher
precedence than <TT>do</TT> <TT>...</TT> <TT>end</TT>. The
following: </P><PRE>identifier1 identifier2 {|varizable| code}</PRE>
<P class=docText>actually means:</P><PRE>identifier1(identifier2 {|variable| code})</PRE>
<P class=docText>On the other hand:</P><PRE>identifier1 identifier2 do |variable| code end</PRE>
<P class=docText>actually means:</P><PRE>identifier1(identifier2) do |variable| code end</PRE><A
name=ch02-51-fm2xml></A><A name=IXT-2-127021></A><A
name=IXT-2-127022></A><A name=IXT-2-127023></A><A
name=IXT-2-127024></A><A name=IXT-2-127025></A><A
name=IXT-2-127026></A><A name=IXT-2-127027></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>def Statement</I></B></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<HR align=left width=515 color=black noShade SIZE=3>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><PRE>def method([<TT><I>arg</I></TT>..., <TT><I>arg</I></TT>=<TT><I>default</I></TT>..., *<TT><I>arg</I></TT>, &<TT><I>arg</I></TT>])
<TT><I>code</I></TT>
[rescue [<TT><I>exception_class</I></TT>[, <TT><I>exception_class</I></TT>...]] [=> <TT><I>variable</I></TT>] [then]
<TT><I>code</I></TT>]...
[else
<TT><I>code</I></TT>]
[ensure
<TT><I>code</I></TT>]
end</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText><A name=IXT-2-127021></A><A
name=IXT-2-127022></A>Defines a method. Arguments may include
the following: </P><A name=IXT-2-127023></A>
<DL class=docList>
<DT><SPAN class=docPubcolor><TT><I>arg</I></TT></SPAN>
<DD>
<P class=docList>Mandatory argument.</P>
<DT><SPAN class=docPubcolor><TT><I>arg</I></TT><SPAN
class=docPubcolor><SPAN class=docMonofont>=</SPAN></SPAN>
<TT><I>default</I></TT></SPAN>
<DD>
<P class=docList>Optional argument. If argument isn't
supplied by that which is calling the method, the
<TT><I>default</I></TT> is assigned to <TT><I>arg</I></TT>.
The <TT><I>default</I></TT> is evaluated at runtime. </P>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>*</SPAN></SPAN> <TT><I>arg</I></TT>
</SPAN>
<DD>
<P class=docList><A name=IXT-2-127023></A>If there are
remaining actual arguments after assigning mandatory and
optional arguments, they are assigned to <TT><I>arg</I></TT>
as an array. If there is no remainder, empty array is
assigned to <TT><I>arg</I></TT>. </P>
<DT><SPAN class=docPubcolor><SPAN class=docPubcolor><SPAN
class=docMonofont>&</SPAN></SPAN> <TT><I>arg</I></TT>
</SPAN>
<DD>
<P class=docList>If the method is invoked with a block, it
is converted to a <TT>Proc</TT> object, then assigned to
<TT><I>arg</I></TT>. Otherwise, <TT>nil</TT> is assigned.
</P></DD></DL>
<P class=docText><A name=IXT-2-127024></A>Operators can also
be specified as method names. For example: </P><PRE>def +(other)
return self.value + other.value
end</PRE>
<P class=docText><A name=IXT-2-127025></A><A
name=IXT-2-127026></A><A name=IXT-2-127027></A>You should
specify <TT>+@</TT> or <TT>-@</TT> for a single plus or minus,
respectively. As with a <TT>begin</TT> block, a method
definition may end with <TT>rescue</TT>, <TT>else</TT>, and
<TT>ensure</TT> clauses. </P><A name=ruby-CHP-2-SECT-7.2></A>
<H4 class=docSection2Title>2.7.2 Singleton Methods</H4><A
name=IXT-2-127028></A>
<P class=docText><A name=IXT-2-127029></A><A
name=IXT-2-127030></A>In Ruby, methods can be defined that are
associated with specific objects only. Such methods are called
singleton methods. Singleton methods are defined using
<TT>def</TT> statements while specifying a receiver. </P>
<P class=docText>Defines a singleton method associated with a
specific object specified by a receiver. The
<TT><I>receiver</I></TT> may be a constant (literal) or an
expression enclosed in parentheses. </P><A
name=ch02-53-fm2xml></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>def Statement for Singleton
Methods</I></B></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<HR align=left width=515 color=black noShade SIZE=3>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><PRE>def
<TT><I>receiver.method</I></TT>([<TT><I>arg</I></TT>...,<TT><I>arg</I></TT>=<TT><I>default</I></TT>..., *<TT><I>arg</I></TT>, &<TT><I>arg</I></TT>])
<TT><I>code</I></TT>
[rescue [<TT><I>exception_class</I></TT>[, <TT><I>exception_class</I></TT>...]] [=> <TT><I>variable</I></TT>] [then]<TT><I>
code</I></TT>]...
[else<TT><I>
code</I></TT>]
[ensure<TT><I>
code</I></TT>]
end
</PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P>
<TABLE cellSpacing=0 cellPadding=1 width="90%" align=center
bgColor=black border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=6 width="100%"
bgColor=white border=0>
<TBODY>
<TR>
<TD vAlign=top width=60><IMG height=54
src="2.7.files/tip_yellow.gif" width=50></TD>
<TD vAlign=top>
<P class=docText><TT>A period</TT> <TT>.</TT>
after <TT><I>receiver</I></TT> can be replaced by
two colons (<TT>::)</TT>. They work the same way,
but <TT>::</TT> is often used for class methods.
</P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></P>
<P class=docText><A name=IXT-2-127031></A><A
name=IXT-2-127032></A>A restriction in the implementation of
Ruby prevents the definition of singleton methods associated
with instances of the <TT>Fixnum</TT> or <TT>Symbol</TT>
class. </P><PRE>a = "foo"
def a.foo
printf "%s(%d)\n", self, self.size
end
a.foo # "foo" is available for a only</PRE><A
name=ruby-CHP-2-SECT-7.3></A>
<H4 class=docSection2Title>2.7.3 Method Operations</H4>
<P class=docText>Not only can you define new methods to
classes and modules, you can also make aliases to the methods
and even remove them from the class. </P><A
name=ch02-57-fm2xml></A><A name=IXT-2-127033></A>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><B><I>alias Statement</I></B></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<HR align=left width=515 color=black noShade SIZE=3>
<TABLE cellPadding=5 width=515 border=0>
<TBODY>
<TR>
<TD align=left><PRE>alias <TT><I>new old</I></TT></PRE></TD>
<TD align=right></TD></TR></TBODY></TABLE>
<P class=docText>Creates an alias <TT><I>new</I></TT> for an
existing method, operator
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -