diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-06-03 12:43:45 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-06-03 12:43:45 +0000 |
commit | a96cf7a7a5d984dcc3b87f297c3f55ff31fd308d (patch) | |
tree | 36dfafe1b40e9a31f11f2f4522a3821b210c79aa /enumerator.c | |
parent | 3064bb9f8d74321ab79d2f37dd40a07d720f4edd (diff) | |
download | ruby-a96cf7a7a5d984dcc3b87f297c3f55ff31fd308d.tar.gz |
* enumerator.c (enumerator_with_memo): New method: with_memo().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16795 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enumerator.c')
-rw-r--r-- | enumerator.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/enumerator.c b/enumerator.c index 13c9bebeca..671f1f74c9 100644 --- a/enumerator.c +++ b/enumerator.c @@ -358,6 +358,42 @@ enumerator_with_index(VALUE obj) } static VALUE +enumerator_with_memo_i(VALUE val, VALUE memo) +{ + return rb_yield_values(2, val, memo); +} + +/* + * call-seq: + * e.with_memo(memo) {|(*args), memo| ... } + * e.with_memo(memo) + * + * Iterates the given block for each elements with an arbitrary + * object given, and returns the memo object. + * + * If no block is given, returns an enumerator. + * + */ +static VALUE +enumerator_with_memo(VALUE obj, VALUE memo) +{ + struct enumerator *e; + int argc = 0; + VALUE *argv = 0; + + RETURN_ENUMERATOR(obj, 0, 0); + e = enumerator_ptr(obj); + if (e->args) { + argc = RARRAY_LEN(e->args); + argv = RARRAY_PTR(e->args); + } + rb_block_call(e->obj, e->meth, argc, argv, + enumerator_with_memo_i, memo); + + return memo; +} + +static VALUE next_ii(VALUE i, VALUE obj, int argc, VALUE *argv) { rb_fiber_yield(argc, argv); @@ -454,6 +490,7 @@ Init_Enumerator(void) rb_define_method(rb_cEnumerator, "each", enumerator_each, 0); rb_define_method(rb_cEnumerator, "each_with_index", enumerator_with_index, 0); rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0); + rb_define_method(rb_cEnumerator, "with_memo", enumerator_with_memo, 1); rb_define_method(rb_cEnumerator, "next", enumerator_next, 0); rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0); |