diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-03-26 03:45:32 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-03-26 03:45:32 +0000 |
commit | 02228688c5086138a20d7c0595230b801dfbdd1a (patch) | |
tree | b2b595f7220967c93887ea559b54df7d3cfe7e72 /enum.c | |
parent | 348ae99e46963615e08f57f168fe57e75660e6fb (diff) |
* enum.c (Enumerable#each_entry): New method.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@27059 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r-- | enum.c | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -1541,6 +1541,48 @@ enum_reverse_each(int argc, VALUE *argv, VALUE obj) static VALUE +each_val_i(i, p) + VALUE i, p; +{ + rb_yield(i); + return Qnil; +} + +/* + * call-seq: + * enum.each_entry {|obj| block} => enum + * + * Calls <i>block</i> once for each element in <i>self</i>, passing that + * element as a parameter, converting multiple values from yield to an + * array. + * + * class Foo + * include Enumerable + * def each + * yield 1 + * yield 1,2 + * end + * end + * Foo.new.each_entry{|o| print o, " -- "} + * + * produces: + * + * 1 -- [1, 2] -- + */ + +static VALUE +enum_each_entry(argc, argv, obj) + int argc; + VALUE *argv; + VALUE obj; +{ + RETURN_ENUMERATOR(obj, argc, argv); + rb_block_call(obj, id_each, argc, argv, each_val_i, 0); + return obj; +} + + +static VALUE zip_i(val, memo) VALUE val; VALUE *memo; @@ -1883,6 +1925,7 @@ Init_Enumerable() rb_define_method(rb_mEnumerable, "each_with_index", enum_each_with_index, 0); rb_define_method(rb_mEnumerable, "enum_with_index", enum_each_with_index, 0); rb_define_method(rb_mEnumerable, "reverse_each", enum_reverse_each, -1); + rb_define_method(rb_mEnumerable, "each_entry", enum_each_entry, -1); rb_define_method(rb_mEnumerable, "zip", enum_zip, -1); rb_define_method(rb_mEnumerable, "take", enum_take, 1); rb_define_method(rb_mEnumerable, "take_while", enum_take_while, 0); |