⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2.7.htm

📁 RUBY的参考手册~~ RUBY的参考手册~~RUBY的参考手册
💻 HTM
📖 第 1 页 / 共 5 页
字号:
            src="2.7.files/spacer.gif" width=1 border=0></TD></TR>
        <TR>
          <TD class=v1 align=middle width=188 height=1><IMG height=1 
            src="2.7.files/spacer.gif" width=1 
      border=0></TD></TR></TBODY></TABLE></DIV></FORM></TD>
    <TD align=middle>
      <TABLE width="95%">
        <TBODY>
        <TR>
          <TD class=v2 align=middle>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD height=15>&nbsp;</TD></TR>
              <TR>
                <TD class=v2 vAlign=top><A 
                  href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&amp;mode=print&amp;sortKey=title&amp;sortOrder=asc&amp;view=&amp;xmlid=0-59600-214-9/ruby-CHP-2-SECT-7&amp;open=false&amp;catid=&amp;s=1&amp;b=1&amp;f=1&amp;t=1&amp;c=1&amp;u=1" 
                  target=_new><IMG height=12 src="2.7.files/img_print.gif" 
                  width=14 align=textTop border=0></A> &nbsp;<A class=v2 
                  title="Printer-friendly version of this section" 
                  href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&amp;mode=print&amp;sortKey=title&amp;sortOrder=asc&amp;view=&amp;xmlid=0-59600-214-9/ruby-CHP-2-SECT-7&amp;open=false&amp;catid=&amp;s=1&amp;b=1&amp;f=1&amp;t=1&amp;c=1&amp;u=1" 
                  target=_new>Print</A> &nbsp;<A 
                  href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&amp;mode=email&amp;sortKey=title&amp;sortOrder=asc&amp;view=&amp;xmlid=0-59600-214-9/ruby-CHP-2-SECT-7&amp;open=false&amp;catid=&amp;s=1&amp;b=1&amp;f=1&amp;t=1&amp;c=1&amp;u=1"><IMG 
                  height=9 hspace=3 src="2.7.files/img_email.gif" width=15 
                  align=absMiddle border=0></A> &nbsp;<A class=v2 
                  title="E-Mail this section" 
                  href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&amp;mode=email&amp;sortKey=title&amp;sortOrder=asc&amp;view=&amp;xmlid=0-59600-214-9/ruby-CHP-2-SECT-7&amp;open=false&amp;catid=&amp;s=1&amp;b=1&amp;f=1&amp;t=1&amp;c=1&amp;u=1">E-Mail</A> 
                  &nbsp;<A 
                  href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&amp;mode=bookmark&amp;sortKey=title&amp;sortOrder=asc&amp;view=&amp;xmlid=0-59600-214-9/ruby-CHP-2-SECT-7&amp;open=false&amp;catid=&amp;s=1&amp;b=1&amp;f=1&amp;t=1&amp;c=1&amp;u=1"><IMG 
                  height=14 hspace=3 src="2.7.files/img_bookmarks.gif" width=6 
                  align=absMiddle border=0></A> &nbsp;<A class=v2 
                  title="Add a bookmark about this section" 
                  href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&amp;mode=bookmark&amp;sortKey=title&amp;sortOrder=asc&amp;view=&amp;xmlid=0-59600-214-9/ruby-CHP-2-SECT-7&amp;open=false&amp;catid=&amp;s=1&amp;b=1&amp;f=1&amp;t=1&amp;c=1&amp;u=1">Add 
                  Bookmark</A></TD>
                <TD class=v2 vAlign=top align=right>&nbsp; <A 
                  href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&amp;mode=section&amp;sortKey=title&amp;sortOrder=asc&amp;view=&amp;xmlid=0-59600-214-9/ruby-CHP-2-SECT-6&amp;open=false&amp;catid=&amp;s=1&amp;b=1&amp;f=1&amp;t=1&amp;c=1&amp;u=1"><IMG 
                  height=16 src="2.7.files/btn_prev.gif" width=56 
                  align=absMiddle border=0></A> &nbsp; <A 
                  href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&amp;mode=section&amp;sortKey=title&amp;sortOrder=asc&amp;view=&amp;xmlid=0-59600-214-9/ruby-CHP-2-SECT-8&amp;open=false&amp;catid=&amp;s=1&amp;b=1&amp;f=1&amp;t=1&amp;c=1&amp;u=1"><IMG 
                  height=16 src="2.7.files/btn_next.gif" width=41 
                  align=absMiddle border=0></A></TD></TR></TBODY></TABLE>
            <HR SIZE=1>

            <TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
              <TBODY>
              <TR>
                <TD vAlign=top width=67 rowSpan=3><IMG 
                  src="2.7.files/ruby_xs.gif" width=67 border=0></TD>
                <TD class=v2 vAlign=top><A class=v1 
                  href="http://proquest.safaribooksonline.com/JVXSL.asp?x=1&amp;mode=section&amp;sortKey=title&amp;sortOrder=asc&amp;view=&amp;xmlid=0-59600-214-9&amp;open=false&amp;catid=&amp;s=1&amp;b=1&amp;f=1&amp;t=1&amp;c=1&amp;u=1&amp;srchText=">Ruby 
                  in a Nutshell</A><BR>By Yukihiro&nbsp;Matsumoto<BR></TD></TR>
              <TR>
                <TD class=v2>&nbsp; </TD></TR>
              <TR>
                <TD class=t1 vAlign=bottom>Chapter 2.&nbsp; Language 
              Basics</TD></TR></TBODY></TABLE>
            <HR SIZE=1>
            <BR>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD vAlign=top><A name=ruby-CHP-2-SECT-7></A>
                  <H3 class=docSection1Title>2.7 Methods</H3><A 
                  name=ruby-IDXTERM-245></A>
                  <P class=docText>Methods are the workhorses of Ruby; all of 
                  your carefully crafted algorithms live in methods on objects 
                  (and classes). In Ruby, "method" means both the named 
                  operation (e.g. "dump") and the code that a specific class 
                  provides to perform an operation. </P>
                  <P class=docText><A name=IXT-2-127006></A><A 
                  name=IXT-2-127007></A>Strictly speaking, Ruby has no 
                  functions, by which I mean code not associated with any 
                  object. (In C++, this is what you might call a "global-scope 
                  function".) All code in Ruby is a method of some object. But 
                  Ruby allows you the flexibility of having some methods appear 
                  and work just like functions in other languages, even though 
                  behind the scenes they're still just methods. </P><A 
                  name=ch02-47-fm2xml></A><A name=IXT-2-127008></A><A 
                  name=IXT-2-127009></A><A name=IXT-2-127010></A><A 
                  name=IXT-2-127011></A><A name=IXT-2-127012></A><A 
                  name=IXT-2-127013></A><A name=IXT-2-127014></A><A 
                  name=IXT-2-127015></A>
                  <TABLE cellPadding=5 width=515 border=0>
                    <TBODY>
                    <TR>
                      <TD align=left><B><I>Normal Method Calls</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><TT><I>obj</I></TT>.<TT><I>method</I></TT>([<TT><I>expr</I></TT>...[, *<TT><I>expr</I></TT>[, &amp;<TT><I>expr</I></TT>]]])
<TT><I>obj</I></TT>.<TT><I>method</I></TT> [<TT><I>expr</I></TT>...[, *<TT><I>expr</I></TT>[, &amp;<TT><I>expr</I></TT>]]]
<TT><I>obj</I></TT>::<TT><I>method</I></TT>([<TT><I>expr</I></TT>...[, *<TT><I>expr</I></TT>[, &amp;<TT><I>expr</I></TT>]]])
<TT><I>obj</I></TT>::<TT><I>method</I></TT> [<TT><I>expr</I></TT>...[, *<TT><I>expr</I></TT>[, &amp;<TT><I>expr</I></TT>]]]</PRE><PRE><TT><I>method</I></TT>([<TT><I>expr</I></TT>...[, *<TT><I>expr</I></TT>[, &amp;<TT><I>expr</I></TT>]]])
<TT><I>method</I></TT> [<TT><I>expr</I></TT>...[,
*<TT><I>expr</I></TT>[, &amp;<TT><I>expr</I></TT>]]]</PRE></TD>
                      <TD align=right></TD></TR></TBODY></TABLE>
                  <P class=docText><A name=IXT-2-127008></A><A 
                  name=IXT-2-127009></A><A name=IXT-2-127010></A>Calls a method. 
                  May take as arguments any number of <TT><I>expr</I></TT> 
                  followed by <TT>*</TT><TT><I>expr</I></TT> and 
                  <TT>&amp;</TT><TT><I>expr</I></TT>. The last expression 
                  argument can be a hash declared directly without braces. 
                  <TT>*</TT><TT><I>expr</I></TT> expands the array value of that 
                  expression and passes it to the method. 
                  <TT>&amp;</TT><TT><I>expr</I></TT> passes the <TT>Proc</TT> 
                  object value of that expression to the method as a block. If 
                  it isn't ambiguous, arguments need not be enclosed in 
                  parentheses. Either <TT>.</TT> or <TT>::</TT> may be used to 
                  separate the object from its method, but it is customary in 
                  Ruby code to use <TT>::</TT> as the separator for class 
                  methods.</P>
                  <P class=docText>Calls a method of <TT>self</TT>. This is the 
                  only form by which private methods may be called.</P>
                  <P class=docText><A name=IXT-2-127011></A><A 
                  name=IXT-2-127012></A>Within modules, module methods and 
                  private instance methods with the same name and definition are 
                  referred to by the general term <SPAN class=docEmphasis>module 
                  functions</SPAN>. This kind of method group can be called in 
                  either of the following ways: </P><PRE>Math.sin(1.0)</PRE>
                  <P class=docText><A name=IXT-2-127013></A><A 
                  name=IXT-2-127014></A><A name=IXT-2-127015></A>or: </P><PRE>include Math
sin(1.0)</PRE>
                  <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>You can append ! or ? to the name 
                              of a Ruby method. Traditionally, <TT>!</TT> is 
                              appended to a method that requires more caution 
                              than the variant of the same name without 
                              <TT>!</TT>. A question mark <TT>?</TT> is appended 
                              to a method that determines the state of a Boolean 
                              value, <TT>true</TT> or <TT>false</TT>.</P>
                              <P class=docText>Attempting to call a method 
                              without specifying either its arguments or 
                              parentheses in a context in which a local variable 
                              of the same name exists results in the method call 
                              being interpreted as a reference to the local 
                              variable, not a call to the method. 
                          </P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></P><A 
                  name=ruby-CHP-2-SECT-7.1></A>
                  <H4 class=docSection2Title>2.7.1 Specifying Blocks with Method 
                  Calls</H4>
                  <P class=docText>Methods may be called with blocks of code 
                  specified that will be called from within the method. </P><PRE><TT><I>method_call</I></TT> {[|[<TT><I>variable</I></TT>[, <TT><I>variable</I></TT>...]]|] <TT><I>code</I></TT>}
<TT><I>method_call</I></TT> do [|[<TT><I>variable</I></TT>[, <TT><I>variable</I></TT>...]]|] <TT><I>code</I></TT> end</PRE>
                  <P class=docText>Calls a method with blocks specified. The 
                  code in the block is executed after a value is passed from the 
                  method to the block and assigned to the variable (the block's 
                  argument) enclosed between <TT>||</TT>. </P>
                  <P class=docText><A name=IXT-2-127016></A><A 
                  name=IXT-2-127017></A><A name=IXT-2-127018></A><A 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -