File: ref_c_proc.xml

package info (click to toggle)
rubybook 0.2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,252 kB
  • ctags: 1,043
  • sloc: xml: 60,486; makefile: 25
file content (228 lines) | stat: -rw-r--r-- 5,651 bytes parent folder | download | duplicates (3)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<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="Proc" super="Object" type="class">
<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>