Skip to content
Newer
Older
100644 2732 lines (2419 sloc) 69.2 KB
fedf489 2000-05-01
matz authored
1 /**********************************************************************
3db12e8 Initial revision
matz authored
2
3 enum.c -
4
5 $Author$
6 created at: Fri Oct 1 15:15:19 JST 1993
7
a25fbe3 * encoding.c: provide basic features for M17N.
matz authored
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
3db12e8 Initial revision
matz authored
9
fedf489 2000-05-01
matz authored
10 **********************************************************************/
3db12e8 Initial revision
matz authored
11
2b59258 @nobu * include/ruby: moved public headers.
nobu authored
12 #include "ruby/ruby.h"
13 #include "ruby/util.h"
2e311f6 @ko1 * include/ruby/node.h, node.h: move node.h from include path.
ko1 authored
14 #include "node.h"
786e4ce @nobu * enum.c: use constants in id.h.
nobu authored
15 #include "id.h"
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
16 #include "internal.h"
17
18 #define STATIC_ASSERT(name, expr) typedef int static_assert_##name##_check[1 - 2*!(expr)]
19
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
20 VALUE rb_mEnumerable;
786e4ce @nobu * enum.c: use constants in id.h.
nobu authored
21 static ID id_next;
22 #define id_each idEach
23 #define id_eqq idEqq
24 #define id_cmp idCmp
f5d724a @nobu * enum.c (id_lshift): use constant ID.
nobu authored
25 #define id_lshift idLTLT
3db12e8 Initial revision
matz authored
26
2baeb78 @shugo * enum.c (rb_enum_values_pack): rename from enum_values_pack, and
shugo authored
27 VALUE
28 rb_enum_values_pack(int argc, VALUE *argv)
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
29 {
30 if (argc == 0) return Qnil;
31 if (argc == 1) return argv[0];
32 return rb_ary_new4(argc, argv);
33 }
34
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
35 #define ENUM_WANT_SVALUE() do { \
2baeb78 @shugo * enum.c (rb_enum_values_pack): rename from enum_values_pack, and
shugo authored
36 i = rb_enum_values_pack(argc, argv); \
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
37 } while (0)
38
1072cb6 @nobu * enum.c (enum_yield): use rb_yield_values2.
nobu authored
39 #define enum_yield rb_yield_values2
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
40
41 static VALUE
01430b6 @nobu * enum.c (enum_grep): gets rid of type-punning calls.
nobu authored
42 grep_i(VALUE i, VALUE args, int argc, VALUE *argv)
3db12e8 Initial revision
matz authored
43 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
44 NODE *memo = RNODE(args);
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
45 ENUM_WANT_SVALUE();
46
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
47 if (RTEST(rb_funcall(memo->u1.value, id_eqq, 1, i))) {
48 rb_ary_push(memo->u2.value, i);
3db12e8 Initial revision
matz authored
49 }
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
50 return Qnil;
3db12e8 Initial revision
matz authored
51 }
52
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
53 static VALUE
01430b6 @nobu * enum.c (enum_grep): gets rid of type-punning calls.
nobu authored
54 grep_iter_i(VALUE i, VALUE args, int argc, VALUE *argv)
3db12e8 Initial revision
matz authored
55 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
56 NODE *memo = RNODE(args);
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
57 ENUM_WANT_SVALUE();
58
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
59 if (RTEST(rb_funcall(memo->u1.value, id_eqq, 1, i))) {
60 rb_ary_push(memo->u2.value, rb_yield(i));
3db12e8 Initial revision
matz authored
61 }
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
62 return Qnil;
3db12e8 Initial revision
matz authored
63 }
64
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
65 /*
66 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
67 * enum.grep(pattern) -> array
68 * enum.grep(pattern) { |obj| block } -> array
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
69 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
70 * Returns an array of every element in <i>enum</i> for which
71 * <code>Pattern === element</code>. If the optional <em>block</em> is
72 * supplied, each matching element is passed to it, and the block's
73 * result is stored in the output array.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
74 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
75 * (1..100).grep 38..44 #=> [38, 39, 40, 41, 42, 43, 44]
76 * c = IO.constants
861219c @akr fix doc.
akr authored
77 * c.grep(/SEEK/) #=> [:SEEK_SET, :SEEK_CUR, :SEEK_END]
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
78 * res = c.grep(/SEEK/) { |v| IO.const_get(v) }
861219c @akr fix doc.
akr authored
79 * res #=> [0, 1, 2]
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
80 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
81 */
82
3db12e8 Initial revision
matz authored
83 static VALUE
dda5dc0 * array.c: moved to ANSI function style from K&R function style.
ocean authored
84 enum_grep(VALUE obj, VALUE pat)
3db12e8 Initial revision
matz authored
85 {
722c202 enum.c: Fix bug in enum_sort_by and some code indents
michal authored
86 VALUE ary = rb_ary_new();
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
87 NODE *memo = NEW_MEMO(pat, ary, 0);
722c202 enum.c: Fix bug in enum_sort_by and some code indents
michal authored
88
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
89 rb_block_call(obj, id_each, 0, 0, rb_block_given_p() ? grep_iter_i : grep_i, (VALUE)memo);
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
90
722c202 enum.c: Fix bug in enum_sort_by and some code indents
michal authored
91 return ary;
3db12e8 Initial revision
matz authored
92 }
93
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
94 static VALUE
3734fd0 * enum.c (count_i): modified to shut warning up.
matz authored
95 count_i(VALUE i, VALUE memop, int argc, VALUE *argv)
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
96 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
97 NODE *memo = RNODE(memop);
f9c5484 @knu * enum.c (count_i, count_iter_i, enum_count, enum_find_index):
knu authored
98
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
99 ENUM_WANT_SVALUE();
100
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
101 if (rb_equal(i, memo->u1.value)) {
102 memo->u3.cnt++;
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
103 }
104 return Qnil;
105 }
106
107 static VALUE
f9c5484 @knu * enum.c (count_i, count_iter_i, enum_count, enum_find_index):
knu authored
108 count_iter_i(VALUE i, VALUE memop, int argc, VALUE *argv)
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
109 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
110 NODE *memo = RNODE(memop);
f9c5484 @knu * enum.c (count_i, count_iter_i, enum_count, enum_find_index):
knu authored
111
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
112 if (RTEST(enum_yield(argc, argv))) {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
113 memo->u3.cnt++;
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
114 }
115 return Qnil;
116 }
117
3a3e250 @knu * enum.c (enum_count, count_all_i, Init_Enumerable),
knu authored
118 static VALUE
119 count_all_i(VALUE i, VALUE memop, int argc, VALUE *argv)
120 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
121 NODE *memo = RNODE(memop);
3a3e250 @knu * enum.c (enum_count, count_all_i, Init_Enumerable),
knu authored
122
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
123 memo->u3.cnt++;
3a3e250 @knu * enum.c (enum_count, count_all_i, Init_Enumerable),
knu authored
124 return Qnil;
125 }
126
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
127 /*
128 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
129 * enum.count -> int
130 * enum.count(item) -> int
131 * enum.count { |obj| block } -> int
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
132 *
38f0dd0 @drbrain * enum.c (enum_count): Enumerable#count no longer uses #size when
drbrain authored
133 * Returns the number of items in +enum+ through enumeration.
134 * If an argument is given, the number of items in +enum+ that
135 * are equal to +item+ are counted. If a block is given, it
136 * counts the number of elements yielding a true value.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
137 *
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
138 * ary = [1, 2, 4, 2]
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
139 * ary.count #=> 4
140 * ary.count(2) #=> 2
141 * ary.count{ |x| x%2==0 } #=> 3
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
142 *
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
143 */
144
145 static VALUE
dda5dc0 * array.c: moved to ANSI function style from K&R function style.
ocean authored
146 enum_count(int argc, VALUE *argv, VALUE obj)
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
147 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
148 VALUE item = Qnil;
149 NODE *memo;
f9c5484 @knu * enum.c (count_i, count_iter_i, enum_count, enum_find_index):
knu authored
150 rb_block_call_func *func;
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
151
f9c5484 @knu * enum.c (count_i, count_iter_i, enum_count, enum_find_index):
knu authored
152 if (argc == 0) {
3a3e250 @knu * enum.c (enum_count, count_all_i, Init_Enumerable),
knu authored
153 if (rb_block_given_p()) {
154 func = count_iter_i;
155 }
156 else {
157 func = count_all_i;
158 }
f9c5484 @knu * enum.c (count_i, count_iter_i, enum_count, enum_find_index):
knu authored
159 }
160 else {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
161 rb_scan_args(argc, argv, "1", &item);
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
162 if (rb_block_given_p()) {
163 rb_warn("given block not used");
164 }
f9c5484 @knu * enum.c (count_i, count_iter_i, enum_count, enum_find_index):
knu authored
165 func = count_i;
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
166 }
167
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
168 memo = NEW_MEMO(item, 0, 0);
169 rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
170 return INT2NUM(memo->u3.cnt);
69ba229 * enum.c (enum_count): new method. [ruby-dev:26895]
matz authored
171 }
172
173 static VALUE
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
174 find_i(VALUE i, VALUE memop, int argc, VALUE *argv)
3db12e8 Initial revision
matz authored
175 {
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
176 ENUM_WANT_SVALUE();
177
178 if (RTEST(rb_yield(i))) {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
179 NODE *memo = RNODE(memop);
180 memo->u1.value = i;
181 memo->u3.cnt = 1;
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
182 rb_iter_break();
3db12e8 Initial revision
matz authored
183 }
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
184 return Qnil;
3db12e8 Initial revision
matz authored
185 }
186
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
187 /*
188 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
189 * enum.detect(ifnone = nil) { |obj| block } -> obj or nil
190 * enum.find(ifnone = nil) { |obj| block } -> obj or nil
191 * enum.detect(ifnone = nil) -> an_enumerator
192 * enum.find(ifnone = nil) -> an_enumerator
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
193 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
194 * Passes each entry in <i>enum</i> to <em>block</em>. Returns the
4afa9ed @marcandre * array.c: Harmonize documentation, in particular regarding:
marcandre authored
195 * first for which <em>block</em> is not false. If no
8e8c7f0 @nobu * enum.c (enum_find): mention about ifnone argument. [ruby-talk:90003]
nobu authored
196 * object matches, calls <i>ifnone</i> and returns its result when it
4afa9ed @marcandre * array.c: Harmonize documentation, in particular regarding:
marcandre authored
197 * is specified, or returns <code>nil</code> otherwise.
198 *
199 * If no block is given, an enumerator is returned instead.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
200 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
201 * (1..10).detect { |i| i % 5 == 0 and i % 7 == 0 } #=> nil
202 * (1..100).detect { |i| i % 5 == 0 and i % 7 == 0 } #=> 35
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
203 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
204 */
205
3db12e8 Initial revision
matz authored
206 static VALUE
dda5dc0 * array.c: moved to ANSI function style from K&R function style.
ocean authored
207 enum_find(int argc, VALUE *argv, VALUE obj)
3db12e8 Initial revision
matz authored
208 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
209 NODE *memo;
3db12e8 Initial revision
matz authored
210 VALUE if_none;
211
212 rb_scan_args(argc, argv, "01", &if_none);
74433fd @nobu * array.c, dir.c, enum.c, hash.c, io.c, range.c, string.c, struct.c:
nobu authored
213 RETURN_ENUMERATOR(obj, argc, argv);
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
214 memo = NEW_MEMO(Qundef, 0, 0);
215 rb_block_call(obj, id_each, 0, 0, find_i, (VALUE)memo);
216 if (memo->u3.cnt) {
217 return memo->u1.value;
3db12e8 Initial revision
matz authored
218 }
219 if (!NIL_P(if_none)) {
5e6634c * parse.y (tokadd_string): ignore backslashed spaces in %w.
matz authored
220 return rb_funcall(if_none, rb_intern("call"), 0, 0);
3db12e8 Initial revision
matz authored
221 }
222 return Qnil;
223 }
224
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
225 static VALUE
f79e928 * enum.c (find_index_i): modified to shut warning up.
matz authored
226 find_index_i(VALUE i, VALUE memop, int argc, VALUE *argv)
033a32b @knu * enum.c (enum_find_index): Add support for find_index(obj);
knu authored
227 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
228 NODE *memo = RNODE(memop);
f79e928 * enum.c (find_index_i): modified to shut warning up.
matz authored
229
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
230 ENUM_WANT_SVALUE();
231
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
232 if (rb_equal(i, memo->u2.value)) {
233 memo->u1.value = UINT2NUM(memo->u3.cnt);
033a32b @knu * enum.c (enum_find_index): Add support for find_index(obj);
knu authored
234 rb_iter_break();
235 }
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
236 memo->u3.cnt++;
033a32b @knu * enum.c (enum_find_index): Add support for find_index(obj);
knu authored
237 return Qnil;
238 }
239
240 static VALUE
f79e928 * enum.c (find_index_i): modified to shut warning up.
matz authored
241 find_index_iter_i(VALUE i, VALUE memop, int argc, VALUE *argv)
6458906 * enum.c (enum_find_index): a new method Enumerable#find_index.
matz authored
242 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
243 NODE *memo = RNODE(memop);
f9c5484 @knu * enum.c (count_i, count_iter_i, enum_count, enum_find_index):
knu authored
244
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
245 if (RTEST(enum_yield(argc, argv))) {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
246 memo->u1.value = UINT2NUM(memo->u3.cnt);
6458906 * enum.c (enum_find_index): a new method Enumerable#find_index.
matz authored
247 rb_iter_break();
248 }
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
249 memo->u3.cnt++;
6458906 * enum.c (enum_find_index): a new method Enumerable#find_index.
matz authored
250 return Qnil;
251 }
252
253 /*
254 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
255 * enum.find_index(value) -> int or nil
256 * enum.find_index { |obj| block } -> int or nil
257 * enum.find_index -> an_enumerator
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
258 *
033a32b @knu * enum.c (enum_find_index): Add support for find_index(obj);
knu authored
259 * Compares each entry in <i>enum</i> with <em>value</em> or passes
260 * to <em>block</em>. Returns the index for the first for which the
261 * evaluated value is non-false. If no object matches, returns
262 * <code>nil</code>
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
263 *
4afa9ed @marcandre * array.c: Harmonize documentation, in particular regarding:
marcandre authored
264 * If neither block nor argument is given, an enumerator is returned instead.
265 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
266 * (1..10).find_index { |i| i % 5 == 0 and i % 7 == 0 } #=> nil
267 * (1..100).find_index { |i| i % 5 == 0 and i % 7 == 0 } #=> 34
033a32b @knu * enum.c (enum_find_index): Add support for find_index(obj);
knu authored
268 * (1..100).find_index(50) #=> 49
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
269 *
6458906 * enum.c (enum_find_index): a new method Enumerable#find_index.
matz authored
270 */
271
272 static VALUE
033a32b @knu * enum.c (enum_find_index): Add support for find_index(obj);
knu authored
273 enum_find_index(int argc, VALUE *argv, VALUE obj)
6458906 * enum.c (enum_find_index): a new method Enumerable#find_index.
matz authored
274 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
275 NODE *memo; /* [return value, current index, ] */
276 VALUE condition_value = Qnil;
033a32b @knu * enum.c (enum_find_index): Add support for find_index(obj);
knu authored
277 rb_block_call_func *func;
6458906 * enum.c (enum_find_index): a new method Enumerable#find_index.
matz authored
278
033a32b @knu * enum.c (enum_find_index): Add support for find_index(obj);
knu authored
279 if (argc == 0) {
280 RETURN_ENUMERATOR(obj, 0, 0);
281 func = find_index_iter_i;
6458906 * enum.c (enum_find_index): a new method Enumerable#find_index.
matz authored
282 }
033a32b @knu * enum.c (enum_find_index): Add support for find_index(obj);
knu authored
283 else {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
284 rb_scan_args(argc, argv, "1", &condition_value);
033a32b @knu * enum.c (enum_find_index): Add support for find_index(obj);
knu authored
285 if (rb_block_given_p()) {
286 rb_warn("given block not used");
287 }
288 func = find_index_i;
289 }
290
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
291 memo = NEW_MEMO(Qnil, condition_value, 0);
033a32b @knu * enum.c (enum_find_index): Add support for find_index(obj);
knu authored
292 rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
293 return memo->u1.value;
6458906 * enum.c (enum_find_index): a new method Enumerable#find_index.
matz authored
294 }
295
296 static VALUE
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
297 find_all_i(VALUE i, VALUE ary, int argc, VALUE *argv)
3db12e8 Initial revision
matz authored
298 {
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
299 ENUM_WANT_SVALUE();
300
301 if (RTEST(rb_yield(i))) {
722c202 enum.c: Fix bug in enum_sort_by and some code indents
michal authored
302 rb_ary_push(ary, i);
3db12e8 Initial revision
matz authored
303 }
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
304 return Qnil;
3db12e8 Initial revision
matz authored
305 }
306
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
307 /*
308 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
309 * enum.find_all { |obj| block } -> array
310 * enum.select { |obj| block } -> array
311 * enum.find_all -> an_enumerator
312 * enum.select -> an_enumerator
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
313 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
314 * Returns an array containing all elements of <i>enum</i> for which
315 * <em>block</em> is not <code>false</code> (see also
316 * <code>Enumerable#reject</code>).
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
317 *
4afa9ed @marcandre * array.c: Harmonize documentation, in particular regarding:
marcandre authored
318 * If no block is given, an enumerator is returned instead.
319 *
320 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
321 * (1..10).find_all { |i| i % 3 == 0 } #=> [3, 6, 9]
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
322 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
323 */
324
3db12e8 Initial revision
matz authored
325 static VALUE
dda5dc0 * array.c: moved to ANSI function style from K&R function style.
ocean authored
326 enum_find_all(VALUE obj)
3db12e8 Initial revision
matz authored
327 {
d725e66 @nobu * enum.c (enumeratorize): create new enumerator for current method if
nobu authored
328 VALUE ary;
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
329
74433fd @nobu * array.c, dir.c, enum.c, hash.c, io.c, range.c, string.c, struct.c:
nobu authored
330 RETURN_ENUMERATOR(obj, 0, 0);
d725e66 @nobu * enum.c (enumeratorize): create new enumerator for current method if
nobu authored
331
332 ary = rb_ary_new();
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
333 rb_block_call(obj, id_each, 0, 0, find_all_i, ary);
3db12e8 Initial revision
matz authored
334
722c202 enum.c: Fix bug in enum_sort_by and some code indents
michal authored
335 return ary;
3db12e8 Initial revision
matz authored
336 }
337
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
338 static VALUE
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
339 reject_i(VALUE i, VALUE ary, int argc, VALUE *argv)
65a5162 1.4.0
matz authored
340 {
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
341 ENUM_WANT_SVALUE();
342
343 if (!RTEST(rb_yield(i))) {
722c202 enum.c: Fix bug in enum_sort_by and some code indents
michal authored
344 rb_ary_push(ary, i);
65a5162 1.4.0
matz authored
345 }
346 return Qnil;
347 }
348
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
349 /*
350 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
351 * enum.reject { |obj| block } -> array
352 * enum.reject -> an_enumerator
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
353 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
354 * Returns an array for all elements of <i>enum</i> for which
355 * <em>block</em> is false (see also <code>Enumerable#find_all</code>).
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
356 *
4afa9ed @marcandre * array.c: Harmonize documentation, in particular regarding:
marcandre authored
357 * If no block is given, an enumerator is returned instead.
358 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
359 * (1..10).reject { |i| i % 3 == 0 } #=> [1, 2, 4, 5, 7, 8, 10]
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
360 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
361 */
362
65a5162 1.4.0
matz authored
363 static VALUE
dda5dc0 * array.c: moved to ANSI function style from K&R function style.
ocean authored
364 enum_reject(VALUE obj)
65a5162 1.4.0
matz authored
365 {
d725e66 @nobu * enum.c (enumeratorize): create new enumerator for current method if
nobu authored
366 VALUE ary;
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
367
74433fd @nobu * array.c, dir.c, enum.c, hash.c, io.c, range.c, string.c, struct.c:
nobu authored
368 RETURN_ENUMERATOR(obj, 0, 0);
d725e66 @nobu * enum.c (enumeratorize): create new enumerator for current method if
nobu authored
369
370 ary = rb_ary_new();
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
371 rb_block_call(obj, id_each, 0, 0, reject_i, ary);
65a5162 1.4.0
matz authored
372
722c202 enum.c: Fix bug in enum_sort_by and some code indents
michal authored
373 return ary;
65a5162 1.4.0
matz authored
374 }
375
376 static VALUE
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
377 collect_i(VALUE i, VALUE ary, int argc, VALUE *argv)
3db12e8 Initial revision
matz authored
378 {
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
379 rb_ary_push(ary, enum_yield(argc, argv));
8e8c7f0 @nobu * enum.c (enum_find): mention about ifnone argument. [ruby-talk:90003]
nobu authored
380
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
381 return Qnil;
3db12e8 Initial revision
matz authored
382 }
383
384 static VALUE
f46baad * enum.c (collect_all): should pack all values. [ruby-core:14410]
matz authored
385 collect_all(VALUE i, VALUE ary, int argc, VALUE *argv)
3db12e8 Initial revision
matz authored
386 {
82f2e90 @nobu * enum.c (collect_all): checks interrupts. [ruby-core:23594]
nobu authored
387 rb_thread_check_ints();
2baeb78 @shugo * enum.c (rb_enum_values_pack): rename from enum_values_pack, and
shugo authored
388 rb_ary_push(ary, rb_enum_values_pack(argc, argv));
8e8c7f0 @nobu * enum.c (enum_find): mention about ifnone argument. [ruby-talk:90003]
nobu authored
389
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
390 return Qnil;
3db12e8 Initial revision
matz authored
391 }
392
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
393 /*
394 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
395 * enum.collect { |obj| block } -> array
396 * enum.map { |obj| block } -> array
397 * enum.collect -> an_enumerator
398 * enum.map -> an_enumerator
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
399 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
400 * Returns a new array with the results of running <em>block</em> once
401 * for every element in <i>enum</i>.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
402 *
4afa9ed @marcandre * array.c: Harmonize documentation, in particular regarding:
marcandre authored
403 * If no block is given, an enumerator is returned instead.
404 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
405 * (1..4).collect { |i| i*i } #=> [1, 4, 9, 16]
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
406 * (1..4).collect { "cat" } #=> ["cat", "cat", "cat", "cat"]
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
407 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
408 */
409
3db12e8 Initial revision
matz authored
410 static VALUE
dda5dc0 * array.c: moved to ANSI function style from K&R function style.
ocean authored
411 enum_collect(VALUE obj)
3db12e8 Initial revision
matz authored
412 {
d725e66 @nobu * enum.c (enumeratorize): create new enumerator for current method if
nobu authored
413 VALUE ary;
8e8c7f0 @nobu * enum.c (enum_find): mention about ifnone argument. [ruby-talk:90003]
nobu authored
414
74433fd @nobu * array.c, dir.c, enum.c, hash.c, io.c, range.c, string.c, struct.c:
nobu authored
415 RETURN_ENUMERATOR(obj, 0, 0);
d725e66 @nobu * enum.c (enumeratorize): create new enumerator for current method if
nobu authored
416
417 ary = rb_ary_new();
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
418 rb_block_call(obj, id_each, 0, 0, collect_i, ary);
3db12e8 Initial revision
matz authored
419
420 return ary;
421 }
422
55bf7f9 * enum.c (enum_flat_map): new method that concatenates the values
matz authored
423 static VALUE
424 flat_map_i(VALUE i, VALUE ary, int argc, VALUE *argv)
425 {
426 VALUE tmp;
427
428 i = enum_yield(argc, argv);
429 tmp = rb_check_array_type(i);
430
431 if (NIL_P(tmp)) {
432 rb_ary_push(ary, i);
433 }
434 else {
435 rb_ary_concat(ary, tmp);
436 }
437 return Qnil;
438 }
439
440 /*
441 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
442 * enum.flat_map { |obj| block } -> array
443 * enum.collect_concat { |obj| block } -> array
444 * enum.flat_map -> an_enumerator
445 * enum.collect_concat -> an_enumerator
55bf7f9 * enum.c (enum_flat_map): new method that concatenates the values
matz authored
446 *
447 * Returns a new array with the concatenated results of running
448 * <em>block</em> once for every element in <i>enum</i>.
449 *
4afa9ed @marcandre * array.c: Harmonize documentation, in particular regarding:
marcandre authored
450 * If no block is given, an enumerator is returned instead.
451 *
3b1276e @akr update doc.
akr authored
452 * [1, 2, 3, 4].flat_map { |e| [e, -e] } #=> [1, -1, 2, -2, 3, -3, 4, -4]
de25f35 @akr update doc.
akr authored
453 * [[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]
55bf7f9 * enum.c (enum_flat_map): new method that concatenates the values
matz authored
454 *
455 */
456
457 static VALUE
458 enum_flat_map(VALUE obj)
459 {
460 VALUE ary;
461
462 RETURN_ENUMERATOR(obj, 0, 0);
463
464 ary = rb_ary_new();
465 rb_block_call(obj, id_each, 0, 0, flat_map_i, ary);
466
467 return ary;
468 }
469
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
470 /*
471 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
472 * enum.to_a -> array
473 * enum.entries -> array
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
474 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
475 * Returns an array containing the items in <i>enum</i>.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
476 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
477 * (1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7]
478 * { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a #=> [["a", 1], ["b", 2], ["c", 3]]
479 */
3db12e8 Initial revision
matz authored
480 static VALUE
fc19d23 @knu * enum.c (enum_to_a): Pass arguments through to #each().
knu authored
481 enum_to_a(int argc, VALUE *argv, VALUE obj)
0d684be 19991029
matz authored
482 {
722c202 enum.c: Fix bug in enum_sort_by and some code indents
michal authored
483 VALUE ary = rb_ary_new();
8e8c7f0 @nobu * enum.c (enum_find): mention about ifnone argument. [ruby-talk:90003]
nobu authored
484
fc19d23 @knu * enum.c (enum_to_a): Pass arguments through to #each().
knu authored
485 rb_block_call(obj, id_each, argc, argv, collect_all, ary);
8c7f7c2 * enum.c (enum_to_a): should propagate taint to the return value.
matz authored
486 OBJ_INFECT(ary, obj);
0d684be 19991029
matz authored
487
e63a990 * re.c (rb_reg_expr_str): should treat backslash specially in
matz authored
488 return ary;
0d684be 19991029
matz authored
489 }
490
491 static VALUE
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
492 inject_i(VALUE i, VALUE p, int argc, VALUE *argv)
87beea4 * enum.c (enum_inject): new method.
matz authored
493 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
494 NODE *memo = RNODE(p);
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
495
496 ENUM_WANT_SVALUE();
497
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
498 if (memo->u2.argc == 0) {
499 memo->u2.argc = 1;
500 memo->u1.value = i;
cecb9ca @nobu * enum.c (enum_inject): use the first iterated element as the
nobu authored
501 }
e323502 @nobu * enum.c (enum_find): catch a value before recycle.
nobu authored
502 else {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
503 memo->u1.value = rb_yield_values(2, memo->u1.value, i);
e323502 @nobu * enum.c (enum_find): catch a value before recycle.
nobu authored
504 }
87beea4 * enum.c (enum_inject): new method.
matz authored
505 return Qnil;
506 }
507
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
508 static VALUE
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
509 inject_op_i(VALUE i, VALUE p, int argc, VALUE *argv)
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
510 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
511 NODE *memo = RNODE(p);
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
512
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
513 ENUM_WANT_SVALUE();
514
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
515 if (memo->u2.argc == 0) {
516 memo->u2.argc = 1;
517 memo->u1.value = i;
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
518 }
519 else {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
520 memo->u1.value = rb_funcall(memo->u1.value, memo->u3.id, 1, i);
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
521 }
522 return Qnil;
523 }
524
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
525 /*
526 * call-seq:
c5b5ab0 @marcandre * enum.c: Documentation: small fixes
marcandre authored
527 * enum.inject(initial, sym) -> obj
c1ecff1 @marcandre * hash.c: Documentation: change => in call-seq to ->.
marcandre authored
528 * enum.inject(sym) -> obj
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
529 * enum.inject(initial) { |memo, obj| block } -> obj
530 * enum.inject { |memo, obj| block } -> obj
c5b5ab0 @marcandre * enum.c: Documentation: small fixes
marcandre authored
531 * enum.reduce(initial, sym) -> obj
c1ecff1 @marcandre * hash.c: Documentation: change => in call-seq to ->.
marcandre authored
532 * enum.reduce(sym) -> obj
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
533 * enum.reduce(initial) { |memo, obj| block } -> obj
534 * enum.reduce { |memo, obj| block } -> obj
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
535 *
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
536 * Combines all elements of <i>enum</i> by applying a binary
8cb190c * enum.c (enum_inject): RDoc update. a patch from David Flanagan
matz authored
537 * operation, specified by a block or a symbol that names a
538 * method or operator.
539 *
fee5e4f @nurse * enum.c (enum_inject): fix typo of rdoc.
nurse authored
540 * If you specify a block, then for each element in <i>enum</i>
8cb190c * enum.c (enum_inject): RDoc update. a patch from David Flanagan
matz authored
541 * the block is passed an accumulator value (<i>memo</i>) and the element.
542 * If you specify a symbol instead, then each element in the collection
543 * will be passed to the named method of <i>memo</i>.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
544 * In either case, the result becomes the new value for <i>memo</i>.
8cb190c * enum.c (enum_inject): RDoc update. a patch from David Flanagan
matz authored
545 * At the end of the iteration, the final value of <i>memo</i> is the
00e84af * enum.c (enum_inject): typo fixed. a patch from Gaku Ueda in
matz authored
546 * return value for the method.
8cb190c * enum.c (enum_inject): RDoc update. a patch from David Flanagan
matz authored
547 *
548 * If you do not explicitly specify an <i>initial</i> value for <i>memo</i>,
e2974b1 @drbrain * enum.c: Clean up wording in Enumerable documentation. Patch by b t.
drbrain authored
549 * then the first element of collection is used as the initial value
8cb190c * enum.c (enum_inject): RDoc update. a patch from David Flanagan
matz authored
550 * of <i>memo</i>.
551 *
552 *
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
553 * # Sum some numbers
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
554 * (5..10).reduce(:+) #=> 45
8cb190c * enum.c (enum_inject): RDoc update. a patch from David Flanagan
matz authored
555 * # Same using a block and inject
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
556 * (5..10).inject { |sum, n| sum + n } #=> 45
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
557 * # Multiply some numbers
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
558 * (5..10).reduce(1, :*) #=> 151200
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
559 * # Same using a block
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
560 * (5..10).inject(1) { |product, n| product * n } #=> 151200
8cb190c * enum.c (enum_inject): RDoc update. a patch from David Flanagan
matz authored
561 * # find the longest word
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
562 * longest = %w{ cat sheep bear }.inject do |memo, word|
8cb190c * enum.c (enum_inject): RDoc update. a patch from David Flanagan
matz authored
563 * memo.length > word.length ? memo : word
564 * end
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
565 * longest #=> "sheep"
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
566 *
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
567 */
87beea4 * enum.c (enum_inject): new method.
matz authored
568 static VALUE
dda5dc0 * array.c: moved to ANSI function style from K&R function style.
ocean authored
569 enum_inject(int argc, VALUE *argv, VALUE obj)
87beea4 * enum.c (enum_inject): new method.
matz authored
570 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
571 NODE *memo;
572 VALUE init, op;
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
573 VALUE (*iter)(VALUE, VALUE, int, VALUE*) = inject_i;
cecb9ca @nobu * enum.c (enum_inject): use the first iterated element as the
nobu authored
574
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
575 switch (rb_scan_args(argc, argv, "02", &init, &op)) {
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
576 case 0:
577 break;
578 case 1:
579 if (rb_block_given_p()) {
3d18c14 @nobu * enum.c (enum_inject): minor improvement. [ruby-dev:30792]
nobu authored
580 break;
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
581 }
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
582 op = (VALUE)rb_to_id(init);
583 argc = 0;
584 init = Qnil;
3d18c14 @nobu * enum.c (enum_inject): minor improvement. [ruby-dev:30792]
nobu authored
585 iter = inject_op_i;
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
586 break;
587 case 2:
588 if (rb_block_given_p()) {
589 rb_warning("given block not used");
590 }
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
591 op = (VALUE)rb_to_id(op);
3d18c14 @nobu * enum.c (enum_inject): minor improvement. [ruby-dev:30792]
nobu authored
592 iter = inject_op_i;
9eec640 * enum.c (enum_inject): it is now can work without block. you
matz authored
593 break;
873b2f9 * enum.c (enum_inject): reuse array for yield parameters.
matz authored
594 }
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
595 memo = NEW_MEMO(init, argc, op);
3d18c14 @nobu * enum.c (enum_inject): minor improvement. [ruby-dev:30792]
nobu authored
596 rb_block_call(obj, id_each, 0, 0, iter, (VALUE)memo);
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
597 return memo->u1.value;
87beea4 * enum.c (enum_inject): new method.
matz authored
598 }
599
600 static VALUE
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
601 partition_i(VALUE i, VALUE arys, int argc, VALUE *argv)
4fa0cde * numeric.c (num_step): better iteration condition for float
matz authored
602 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
603 NODE *memo = RNODE(arys);
604 VALUE ary;
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
605 ENUM_WANT_SVALUE();
606
607 if (RTEST(rb_yield(i))) {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
608 ary = memo->u1.value;
4fa0cde * numeric.c (num_step): better iteration condition for float
matz authored
609 }
610 else {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
611 ary = memo->u2.value;
4fa0cde * numeric.c (num_step): better iteration condition for float
matz authored
612 }
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
613 rb_ary_push(ary, i);
4fa0cde * numeric.c (num_step): better iteration condition for float
matz authored
614 return Qnil;
615 }
616
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
617 /*
618 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
619 * enum.partition { |obj| block } -> [ true_array, false_array ]
620 * enum.partition -> an_enumerator
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
621 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
622 * Returns two arrays, the first containing the elements of
623 * <i>enum</i> for which the block evaluates to true, the second
624 * containing the rest.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
625 *
4afa9ed @marcandre * array.c: Harmonize documentation, in particular regarding:
marcandre authored
626 * If no block is given, an enumerator is returned instead.
627 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
628 * (1..6).partition { |v| v.even? } #=> [[2, 4, 6], [1, 3, 5]]
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
629 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
630 */
631
4fa0cde * numeric.c (num_step): better iteration condition for float
matz authored
632 static VALUE
dda5dc0 * array.c: moved to ANSI function style from K&R function style.
ocean authored
633 enum_partition(VALUE obj)
4fa0cde * numeric.c (num_step): better iteration condition for float
matz authored
634 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
635 NODE *memo;
4fa0cde * numeric.c (num_step): better iteration condition for float
matz authored
636
74433fd @nobu * array.c, dir.c, enum.c, hash.c, io.c, range.c, string.c, struct.c:
nobu authored
637 RETURN_ENUMERATOR(obj, 0, 0);
d725e66 @nobu * enum.c (enumeratorize): create new enumerator for current method if
nobu authored
638
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
639 memo = NEW_MEMO(rb_ary_new(), rb_ary_new(), 0);
640 rb_block_call(obj, id_each, 0, 0, partition_i, (VALUE)memo);
4fa0cde * numeric.c (num_step): better iteration condition for float
matz authored
641
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
642 return rb_assoc_new(memo->u1.value, memo->u2.value);
4fa0cde * numeric.c (num_step): better iteration condition for float
matz authored
643 }
644
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
645 static VALUE
54cf2fc * enum.c (enum_yield): when multiple values yielded from #each
matz authored
646 group_by_i(VALUE i, VALUE hash, int argc, VALUE *argv)
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
647 {
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
648 VALUE group;
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
649 VALUE values;
650
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
651 ENUM_WANT_SVALUE();
652
653 group = rb_yield(i);
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
654 values = rb_hash_aref(hash, group);
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
655 if (!RB_TYPE_P(values, T_ARRAY)) {
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
656 values = rb_ary_new3(1, i);
657 rb_hash_aset(hash, group, values);
658 }
659 else {
660 rb_ary_push(values, i);
661 }
662 return Qnil;
663 }
664
665 /*
666 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
667 * enum.group_by { |obj| block } -> a_hash
668 * enum.group_by -> an_enumerator
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
669 *
8f6cb4b @drbrain * enum.c (group_by): Improve documentation based on patch by b t.
drbrain authored
670 * Groups the collection by result of the block. Returns a hash where the
82f1f64 @drbrain * enum.c (group_by): Improve group_by description. Patch by b t. [#5411]
drbrain authored
671 * keys are the evaluated result from the block and the values are
672 * arrays of elements in the collection that correspond to the key.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
673 *
8f6cb4b @drbrain * enum.c (group_by): Improve documentation based on patch by b t.
drbrain authored
674 * If no block is given an enumerator is returned.
4afa9ed @marcandre * array.c: Harmonize documentation, in particular regarding:
marcandre authored
675 *
82f1f64 @drbrain * enum.c (group_by): Improve group_by description. Patch by b t. [#5411]
drbrain authored
676 * (1..6).group_by { |i| i%3 } #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
677 *
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
678 */
679
680 static VALUE
681 enum_group_by(VALUE obj)
682 {
683 VALUE hash;
684
685 RETURN_ENUMERATOR(obj, 0, 0);
686
687 hash = rb_hash_new();
688 rb_block_call(obj, id_each, 0, 0, group_by_i, hash);
8c7f7c2 * enum.c (enum_to_a): should propagate taint to the return value.
matz authored
689 OBJ_INFECT(hash, obj);
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
690
691 return hash;
692 }
693
694 static VALUE
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
695 first_i(VALUE i, VALUE params, int argc, VALUE *argv)
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
696 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
697 NODE *memo = RNODE(params);
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
698 ENUM_WANT_SVALUE();
699
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
700 memo->u1.value = i;
701 rb_iter_break();
e95f7ea @drbrain * variable.c (trace_ev): Removed "not reached" comment as this line is
drbrain authored
702
703 UNREACHABLE;
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
704 }
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
705
a920c29 @nobu * enum.c (enum_first): remove duplication.
nobu authored
706 static VALUE enum_take(VALUE obj, VALUE n);
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
707
708 /*
709 * call-seq:
c1ecff1 @marcandre * hash.c: Documentation: change => in call-seq to ->.
marcandre authored
710 * enum.first -> obj or nil
711 * enum.first(n) -> an_array
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
712 *
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
713 * Returns the first element, or the first +n+ elements, of the enumerable.
714 * If the enumerable is empty, the first form returns <code>nil</code>, and the
715 * second form returns an empty array.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
716 *
e8fc308 @akr update doc.
akr authored
717 * %w[foo bar baz].first #=> "foo"
718 * %w[foo bar baz].first(2) #=> ["foo", "bar"]
719 * %w[foo bar baz].first(10) #=> ["foo", "bar", "baz"]
720 * [].first #=> nil
721 *
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
722 */
723
724 static VALUE
725 enum_first(int argc, VALUE *argv, VALUE obj)
726 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
727 NODE *memo;
a920c29 @nobu * enum.c (enum_first): remove duplication.
nobu authored
728 rb_check_arity(argc, 0, 1);
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
729 if (argc > 0) {
a920c29 @nobu * enum.c (enum_first): remove duplication.
nobu authored
730 return enum_take(obj, argv[0]);
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
731 }
732 else {
733 memo = NEW_MEMO(Qnil, 0, 0);
734 rb_block_call(obj, id_each, 0, 0, first_i, (VALUE)memo);
735 return memo->u1.value;
5675cdb * eval.c: unify ruby_class (for method definition) and ruby_cbase
matz authored
736 }
737 }
738
d2a1637 * enum.c (enum_take_while): separate with-block form.
matz authored
739
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
740 /*
741 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
742 * enum.sort -> array
743 * enum.sort { |a, b| block } -> array
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
744 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
745 * Returns an array containing the items in <i>enum</i> sorted,
746 * either according to their own <code><=></code> method, or by using
747 * the results of the supplied block. The block should return -1, 0, or
748 * +1 depending on the comparison between <i>a</i> and <i>b</i>. As of
749 * Ruby 1.8, the method <code>Enumerable#sort_by</code> implements a
750 * built-in Schwartzian Transform, useful when key computation or
0ff9cf3 @akr fix rdoc.
akr authored
751 * comparison is expensive.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
752 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
753 * %w(rhea kea flea).sort #=> ["flea", "kea", "rhea"]
754 * (1..10).sort { |a, b| b <=> a } #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
755 */
756
4fa0cde * numeric.c (num_step): better iteration condition for float
matz authored
757 static VALUE
dda5dc0 * array.c: moved to ANSI function style from K&R function style.
ocean authored
758 enum_sort(VALUE obj)
3db12e8 Initial revision
matz authored
759 {
fc19d23 @knu * enum.c (enum_to_a): Pass arguments through to #each().
knu authored
760 return rb_ary_sort(enum_to_a(0, 0, obj));
3db12e8 Initial revision
matz authored
761 }
762
74b08ff @akr * enum.c (enum_sort_by): use less temporary objects.
akr authored
763 #define SORT_BY_BUFSIZE 16
764 struct sort_by_data {
765 VALUE ary;
766 VALUE buf;
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
767 long n;
74b08ff @akr * enum.c (enum_sort_by): use less temporary objects.
akr authored
768 };
769
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
770 static VALUE
74b08ff @akr * enum.c (enum_sort_by): use less temporary objects.
akr authored
771 sort_by_i(VALUE i, VALUE _data, int argc, VALUE *argv)
1289a7a * eval.c (is_defined): should not dump core for "defined?(())".
matz authored
772 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
773 struct sort_by_data *data = (struct sort_by_data *)&RNODE(_data)->u1;
74b08ff @akr * enum.c (enum_sort_by): use less temporary objects.
akr authored
774 VALUE ary = data->ary;
e1680f8 @akr * enum.c (sort_by_i): reenter check more strictly.
akr authored
775 VALUE v;
186c8b5 * marshal.c (w_float): must distinguish -0.0 from 0.0.
matz authored
776
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
777 ENUM_WANT_SVALUE();
778
e1680f8 @akr * enum.c (sort_by_i): reenter check more strictly.
akr authored
779 v = rb_yield(i);
780
c800d0b * io.c (rb_fopen): mode string copy at the lowest level.
matz authored
781 if (RBASIC(ary)->klass) {
782 rb_raise(rb_eRuntimeError, "sort_by reentered");
783 }
e1680f8 @akr * enum.c (sort_by_i): reenter check more strictly.
akr authored
784 if (RARRAY_LEN(data->buf) != SORT_BY_BUFSIZE*2) {
785 rb_raise(rb_eRuntimeError, "sort_by reentered");
786 }
787
788 RARRAY_PTR(data->buf)[data->n*2] = v;
74b08ff @akr * enum.c (enum_sort_by): use less temporary objects.
akr authored
789 RARRAY_PTR(data->buf)[data->n*2+1] = i;
790 data->n++;
791 if (data->n == SORT_BY_BUFSIZE) {
792 rb_ary_concat(ary, data->buf);
793 data->n = 0;
794 }
1289a7a * eval.c (is_defined): should not dump core for "defined?(())".
matz authored
795 return Qnil;
796 }
797
1057902 * io.c (read_all): block string buffer modification during
matz authored
798 static int
1b03efe @nobu * array.c, enum.c, eval.c, util.c: safer function pointer usage.
nobu authored
799 sort_by_cmp(const void *ap, const void *bp, void *data)
1289a7a * eval.c (is_defined): should not dump core for "defined?(())".
matz authored
800 {
e1680f8 @akr * enum.c (sort_by_i): reenter check more strictly.
akr authored
801 VALUE a;
802 VALUE b;
a594151 @nobu * enum.c (sort_by_cmp): check if reentered. [ruby-dev:24291]
nobu authored
803 VALUE ary = (VALUE)data;
2f8d3bd * array.c (rb_ary_modify): should copy the internal buffer if the
matz authored
804
a594151 @nobu * enum.c (sort_by_cmp): check if reentered. [ruby-dev:24291]
nobu authored
805 if (RBASIC(ary)->klass) {
806 rb_raise(rb_eRuntimeError, "sort_by reentered");
807 }
e1680f8 @akr * enum.c (sort_by_i): reenter check more strictly.
akr authored
808
809 a = *(VALUE *)ap;
810 b = *(VALUE *)bp;
811
1057902 * io.c (read_all): block string buffer modification during
matz authored
812 return rb_cmpint(rb_funcall(a, id_cmp, 1, b), a, b);
1289a7a * eval.c (is_defined): should not dump core for "defined?(())".
matz authored
813 }
814
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
815 /*
816 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
817 * enum.sort_by { |obj| block } -> array
818 * enum.sort_by -> an_enumerator
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
819 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
820 * Sorts <i>enum</i> using a set of keys generated by mapping the
821 * values in <i>enum</i> through the given block.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
822 *
4afa9ed @marcandre * array.c: Harmonize documentation, in particular regarding:
marcandre authored
823 * If no block is given, an enumerator is returned instead.
824 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
825 * %w{apple pear fig}.sort_by { |word| word.length}
861219c @akr fix doc.
akr authored
826 * #=> ["fig", "pear", "apple"]
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
827 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
828 * The current implementation of <code>sort_by</code> generates an
829 * array of tuples containing the original collection element and the
830 * mapped value. This makes <code>sort_by</code> fairly expensive when
e2974b1 @drbrain * enum.c: Clean up wording in Enumerable documentation. Patch by b t.
drbrain authored
831 * the keysets are simple.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
832 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
833 * require 'benchmark'
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
834 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
835 * a = (1..100000).map { rand(100000) }
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
836 *
4afa9ed @marcandre * array.c: Harmonize documentation, in particular regarding:
marcandre authored
837 * Benchmark.bm(10) do |b|
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
838 * b.report("Sort") { a.sort }
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
839 * b.report("Sort by") { a.sort_by { |a| a } }
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
840 * end
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
841 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
842 * <em>produces:</em>
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
843 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
844 * user system total real
845 * Sort 0.180000 0.000000 0.180000 ( 0.175469)
846 * Sort by 1.980000 0.040000 2.020000 ( 2.013586)
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
847 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
848 * However, consider the case where comparing the keys is a non-trivial
849 * operation. The following code sorts some files on modification time
850 * using the basic <code>sort</code> method.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
851 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
852 * files = Dir["*"]
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
853 * sorted = files.sort { |a, b| File.new(a).mtime <=> File.new(b).mtime }
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
854 * sorted #=> ["mon", "tues", "wed", "thurs"]
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
855 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
856 * This sort is inefficient: it generates two new <code>File</code>
857 * objects during every comparison. A slightly better technique is to
858 * use the <code>Kernel#test</code> method to generate the modification
859 * times directly.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
860 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
861 * files = Dir["*"]
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
862 * sorted = files.sort { |a, b|
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
863 * test(?M, a) <=> test(?M, b)
864 * }
865 * sorted #=> ["mon", "tues", "wed", "thurs"]
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
866 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
867 * This still generates many unnecessary <code>Time</code> objects. A
868 * more efficient technique is to cache the sort keys (modification
869 * times in this case) before the sort. Perl users often call this
870 * approach a Schwartzian Transform, after Randal Schwartz. We
871 * construct a temporary array, where each element is an array
872 * containing our sort key along with the filename. We sort this array,
873 * and then extract the filename from the result.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
874 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
875 * sorted = Dir["*"].collect { |f|
876 * [test(?M, f), f]
877 * }.sort.collect { |f| f[1] }
878 * sorted #=> ["mon", "tues", "wed", "thurs"]
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
879 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
880 * This is exactly what <code>sort_by</code> does internally.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
881 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
882 * sorted = Dir["*"].sort_by { |f| test(?M, f) }
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
883 * sorted #=> ["mon", "tues", "wed", "thurs"]
884 */
885
1289a7a * eval.c (is_defined): should not dump core for "defined?(())".
matz authored
886 static VALUE
dda5dc0 * array.c: moved to ANSI function style from K&R function style.
ocean authored
887 enum_sort_by(VALUE obj)
1289a7a * eval.c (is_defined): should not dump core for "defined?(())".
matz authored
888 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
889 VALUE ary, buf;
890 NODE *memo;
1289a7a * eval.c (is_defined): should not dump core for "defined?(())".
matz authored
891 long i;
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
892 struct sort_by_data *data;
1289a7a * eval.c (is_defined): should not dump core for "defined?(())".
matz authored
893
74433fd @nobu * array.c, dir.c, enum.c, hash.c, io.c, range.c, string.c, struct.c:
nobu authored
894 RETURN_ENUMERATOR(obj, 0, 0);
d725e66 @nobu * enum.c (enumeratorize): create new enumerator for current method if
nobu authored
895
8e6e8e6 @nobu * use RB_TYPE_P which is optimized for constant types, instead of
nobu authored
896 if (RB_TYPE_P(obj, T_ARRAY) && RARRAY_LEN(obj) <= LONG_MAX/2) {
5a17f4c @akr fix spaces.
akr authored
897 ary = rb_ary_new2(RARRAY_LEN(obj)*2);
7e35911 * ext/pty/pty.c (pty_finalize_syswait): join (using Thread#value)
matz authored
898 }
899 else {
900 ary = rb_ary_new();
901 }
779f3d6 * string.c (rb_str_sum): wrong cast caused wrong result.
matz authored
902 RBASIC(ary)->klass = 0;
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
903 buf = rb_ary_tmp_new(SORT_BY_BUFSIZE*2);
904 rb_ary_store(buf, SORT_BY_BUFSIZE*2-1, Qnil);
905 memo = NEW_MEMO(0, 0, 0);
906 OBJ_INFECT(memo, obj);
907 data = (struct sort_by_data *)&memo->u1;
908 data->ary = ary;
909 data->buf = buf;
910 data->n = 0;
911 rb_block_call(obj, id_each, 0, 0, sort_by_i, (VALUE)memo);
912 ary = data->ary;
913 buf = data->buf;
914 if (data->n) {
915 rb_ary_resize(buf, data->n*2);
916 rb_ary_concat(ary, buf);
74b08ff @akr * enum.c (enum_sort_by): use less temporary objects.
akr authored
917 }
918 if (RARRAY_LEN(ary) > 2) {
919 ruby_qsort(RARRAY_PTR(ary), RARRAY_LEN(ary)/2, 2*sizeof(VALUE),
a594151 @nobu * enum.c (sort_by_cmp): check if reentered. [ruby-dev:24291]
nobu authored
920 sort_by_cmp, (void *)ary);
b3ecbfa * eval.c (svalue_to_avalue): v may be Qundef. This fix was
matz authored
921 }
3e9e2bd * eval.c (rb_eval): NODE_XSTR should pass copy of literal string.
matz authored
922 if (RBASIC(ary)->klass) {
923 rb_raise(rb_eRuntimeError, "sort_by reentered");
924 }
74b08ff @akr * enum.c (enum_sort_by): use less temporary objects.
akr authored
925 for (i=1; i<RARRAY_LEN(ary); i+=2) {
926 RARRAY_PTR(ary)[i/2] = RARRAY_PTR(ary)[i];
1289a7a * eval.c (is_defined): should not dump core for "defined?(())".
matz authored
927 }
113f27b @akr * enum.c (enum_sort_by): use rb_ary_resize.
akr authored
928 rb_ary_resize(ary, RARRAY_LEN(ary)/2);
779f3d6 * string.c (rb_str_sum): wrong cast caused wrong result.
matz authored
929 RBASIC(ary)->klass = rb_cArray;
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
930 OBJ_INFECT(ary, memo);
8c7f7c2 * enum.c (enum_to_a): should propagate taint to the return value.
matz authored
931
1289a7a * eval.c (is_defined): should not dump core for "defined?(())".
matz authored
932 return ary;
933 }
934
0f2d152 @nobu * enum.c (DEFINE_ENUMFUNCS): included function signature.
nobu authored
935 #define ENUMFUNC(name) rb_block_given_p() ? name##_iter_i : name##_i
936
1072cb6 @nobu * enum.c (enum_yield): use rb_yield_values2.
nobu authored
937 #define DEFINE_ENUMFUNCS(name) \
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
938 static VALUE enum_##name##_func(VALUE result, NODE *memo); \
0f2d152 @nobu * enum.c (DEFINE_ENUMFUNCS): included function signature.
nobu authored
939 \
1072cb6 @nobu * enum.c (enum_yield): use rb_yield_values2.
nobu authored
940 static VALUE \
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
941 name##_i(VALUE i, VALUE memo, int argc, VALUE *argv) \
1072cb6 @nobu * enum.c (enum_yield): use rb_yield_values2.
nobu authored
942 { \
2baeb78 @shugo * enum.c (rb_enum_values_pack): rename from enum_values_pack, and
shugo authored
943 return enum_##name##_func(rb_enum_values_pack(argc, argv), RNODE(memo)); \
1072cb6 @nobu * enum.c (enum_yield): use rb_yield_values2.
nobu authored
944 } \
945 \
946 static VALUE \
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
947 name##_iter_i(VALUE i, VALUE memo, int argc, VALUE *argv) \
1072cb6 @nobu * enum.c (enum_yield): use rb_yield_values2.
nobu authored
948 { \
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
949 return enum_##name##_func(enum_yield(argc, argv), RNODE(memo)); \
0f2d152 @nobu * enum.c (DEFINE_ENUMFUNCS): included function signature.
nobu authored
950 } \
951 \
952 static VALUE \
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
953 enum_##name##_func(VALUE result, NODE *memo)
12d2c8b @nobu stripped trailing spaces.
nobu authored
954
0f2d152 @nobu * enum.c (DEFINE_ENUMFUNCS): included function signature.
nobu authored
955 DEFINE_ENUMFUNCS(all)
a973f96 * enum.c (enum_all): now works without block.
matz authored
956 {
1072cb6 @nobu * enum.c (enum_yield): use rb_yield_values2.
nobu authored
957 if (!RTEST(result)) {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
958 memo->u1.value = Qfalse;
d701d21 @knu * enum.c (grep_i): Be aware of multiple values;
knu authored
959 rb_iter_break();
960 }
961 return Qnil;
a973f96 * enum.c (enum_all): now works without block.
matz authored
962 }
963
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
964 /*
965 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
966 * enum.all? [{ |obj| block } ] -> true or false
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
967 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
968 * Passes each element of the collection to the given block. The method
969 * returns <code>true</code> if the block never returns
970 * <code>false</code> or <code>nil</code>. If the block is not given,
e2974b1 @drbrain * enum.c: Clean up wording in Enumerable documentation. Patch by b t.
drbrain authored
971 * Ruby adds an implicit block of <code>{ |obj| obj }</code> which will
972 * cause #all? to return +true+ when none of the collection members are
973 * +false+ or +nil+.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
974 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
975 * %w[ant bear cat].all? { |word| word.length >= 3 } #=> true
976 * %w[ant bear cat].all? { |word| word.length >= 4 } #=> false
977 * [nil, true, 99].all? #=> false
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
978 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
979 */
980
a973f96 * enum.c (enum_all): now works without block.
matz authored
981 static VALUE
dda5dc0 * array.c: moved to ANSI function style from K&R function style.
ocean authored
982 enum_all(VALUE obj)
e502549 * io.c (argf_seek_m): wrong calling sequence of rb_io_seek().
matz authored
983 {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
984 NODE *memo = NEW_MEMO(Qtrue, 0, 0);
985 rb_block_call(obj, id_each, 0, 0, ENUMFUNC(all), (VALUE)memo);
986 return memo->u1.value;
e502549 * io.c (argf_seek_m): wrong calling sequence of rb_io_seek().
matz authored
987 }
988
0f2d152 @nobu * enum.c (DEFINE_ENUMFUNCS): included function signature.
nobu authored
989 DEFINE_ENUMFUNCS(any)
e502549 * io.c (argf_seek_m): wrong calling sequence of rb_io_seek().
matz authored
990 {
1072cb6 @nobu * enum.c (enum_yield): use rb_yield_values2.
nobu authored
991 if (RTEST(result)) {
41e331c @nobu * enum.c: move work variables to objects not to let called blocks
nobu authored
992 memo->u1.value = Qtrue;
e502549 * io.c (argf_seek_m): wrong calling sequence of rb_io_seek().
matz authored
993 rb_iter_break();
994 }
995 return Qnil;
996 }
997
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
998 /*
999 * call-seq:
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored
1000 * enum.any? [{ |obj| block }] -> true or false
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
1001 *
84f0b05 Annotate enum.c. Add pager support, and report on methods in included…
dave authored
1002 * Passes each element of the collection to the given block. The method
1003 * returns <code>true</code> if the block ever returns a value other
bb58656 @drbrain Documentation typo.
drbrain authored
1004 * than <code>false</code> or <code>nil</code>. If the block is not
e2974b1 @drbrain * enum.c: Clean up wording in Enumerable documentation. Patch by b t.
drbrain authored
1005 * given, Ruby adds an implicit block of <code>{ |obj| obj }</code> that
1006 * will cause #any? to return +true+ if at least one of the collection
1007 * members is not +false+ or +nil+.
93df43f @nobu * enum.c: removed trailing garbage spaces.
nobu authored
1008 *
2a4ce07 @drbrain * enum.c: Reformat block args to a single standard, { |args| ... }.
drbrain authored