<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="Proc" super="Object" type="class" ref="xref-000001">
<p/>
  <tt>Proc</tt> objects are blocks of code that have been bound to a set
  of local variables. Once bound, the code may be called in different
  contexts and still access those variables.
<p/>
<codefragment>
<fullcode><![CDATA[    def genTimes(factor)
      return Proc.new {|n| n*factor }
    end

    times3 = genTimes(3)
    times5 = genTimes(5)

    times3.call(12)
    times5.call(5)
    times3.call(times5.call(4))
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>def<nbsp/>genTimes(factor)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>return<nbsp/>Proc.new<nbsp/>{|n|<nbsp/>n*factor<nbsp/>}</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt></tt></td>
</tr>
<tr>
<td colspan="3"><tt>times3<nbsp/>=<nbsp/>genTimes(3)</tt></td>
</tr>
<tr>
<td colspan="3"><tt>times5<nbsp/>=<nbsp/>genTimes(5)</tt></td>
</tr>
<tr>
<td colspan="3"><tt></tt></td>
</tr>
<tr>
  <td><tt>times3.call(12)</tt></td>
  <td>&#187;</td>
  <td><tt>36</tt></td>
</tr>
<tr>
  <td><tt>times5.call(5)</tt></td>
  <td>&#187;</td>
  <td><tt>25</tt></td>
</tr>
<tr>
  <td><tt>times3.call(times5.call(4))</tt></td>
  <td>&#187;</td>
  <td><tt>60</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
  <methods type="class">
<p/>
        <method name="new" ref="new">
      <callseq>
        Proc.new <opt><block>{|  | <blockbody>block</blockbody> }</block>
</opt>
        <returns><obj>aProc</obj></returns>
      </callseq>
      <desc>
<p/>
      Creates a new <tt>Proc</tt> object, bound to the current context. It
      may be called without a block only within a method with an
      attached block, in which case that block is converted to the
      <classname>Proc</classname> object.
<p/>
<codefragment>
<fullcode><![CDATA[        def procFrom
          Proc.new
        end
        aProc = procFrom { "hello" }
        aProc.call
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>def<nbsp/>procFrom</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>Proc.new</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt>aProc<nbsp/>=<nbsp/>procFrom<nbsp/>{<nbsp/>"hello"<nbsp/>}</tt></td>
</tr>
<tr>
  <td><tt>aProc.call</tt></td>
  <td>&#187;</td>
  <td><tt>"hello"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
      </desc>
    </method>
<p/>
  </methods>
<p/>
  <methods type="instance">
<p/>
        <method name="[ ]" ref="_ob_cb">
      <callseq>
        <obj>prc</obj>[ <optz><obj>params</obj></optz> ]
        <returns><obj>anObject</obj></returns>
      </callseq>
      <desc>
<p/>
      Synonym for <tt>Proc.call</tt>.
<p/>
      </desc>
    </method>
<p/>
        <method name="arity" ref="arity">
      <callseq>
        <obj>prc</obj>.arity <returns><obj>anInteger</obj></returns>
      </callseq>
      <desc>
<p/>
      Returns the number of arguments required by the block. If the
      block takes no arguments, returns -1. If it takes one argument,
      returns -2. Otherwise, returns a positive argument count unless
      the last argument is prefixed with *, in which case the argument 
      count is negated. The number of required arguments is
      <obj>anInteger</obj> for positive values, and 
      <tt>(</tt><obj>anInteger</obj><tt>+1).abs</tt> otherwise.
<p/>
<codefragment>
<fullcode><![CDATA[        Proc.new {||}.arity
        Proc.new {|a|}.arity
        Proc.new {|a,b|}.arity
        Proc.new {|a,b,c|}.arity
        Proc.new {|*a|}.arity
        Proc.new {|a,*b|}.arity
]]></fullcode><rubycode>
<tr>
  <td><tt>Proc.new<nbsp/>{||}.arity</tt></td>
  <td>&#187;</td>
  <td><tt>0</tt></td>
</tr>
<tr>
  <td><tt>Proc.new<nbsp/>{|a|}.arity</tt></td>
  <td>&#187;</td>
  <td><tt>-1</tt></td>
</tr>
<tr>
  <td><tt>Proc.new<nbsp/>{|a,b|}.arity</tt></td>
  <td>&#187;</td>
  <td><tt>2</tt></td>
</tr>
<tr>
  <td><tt>Proc.new<nbsp/>{|a,b,c|}.arity</tt></td>
  <td>&#187;</td>
  <td><tt>3</tt></td>
</tr>
<tr>
  <td><tt>Proc.new<nbsp/>{|*a|}.arity</tt></td>
  <td>&#187;</td>
  <td><tt>-1</tt></td>
</tr>
<tr>
  <td><tt>Proc.new<nbsp/>{|a,*b|}.arity</tt></td>
  <td>&#187;</td>
  <td><tt>-2</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
      </desc>
    </method>
<p/>
        <method name="call" ref="call">
      <callseq>
        <obj>prc</obj>.call( <optz><obj>params</obj></optz> )
        <returns><obj>anObject</obj></returns>
      </callseq>
      <desc>
<p/>
      Invokes the block, setting the block's parameters to the values
      in <obj>params</obj> using the same rules as used by parallel
      assignment. Returns the value of the last expression evaluated
      in the block.
<p/>
<codefragment>
<fullcode><![CDATA[         aProc = Proc.new {|a, *b| b.collect {|i| i*a }}
         aProc.call(9, 1, 2, 3)
         aProc[9, 1, 2, 3] 
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>aProc<nbsp/>=<nbsp/>Proc.new<nbsp/>{|a,<nbsp/>*b|<nbsp/>b.collect<nbsp/>{|i|<nbsp/>i*a<nbsp/>}}</tt></td>
</tr>
<tr>
  <td><tt>aProc.call(9,<nbsp/>1,<nbsp/>2,<nbsp/>3)</tt></td>
  <td>&#187;</td>
  <td><tt>[9,<nbsp/>18,<nbsp/>27]</tt></td>
</tr>
<tr>
  <td><tt>aProc[9,<nbsp/>1,<nbsp/>2,<nbsp/>3]</tt></td>
  <td>&#187;</td>
  <td><tt>[9,<nbsp/>18,<nbsp/>27]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
      </desc>
    </method>
<p/>
  </methods>
<p/>
</class>
</ppdoc>
