From 76326044c8cd17348e178ecf1fdc5b9bb91152dc Mon Sep 17 00:00:00 2001 From: Franck Verrot Date: Wed, 12 Nov 2014 05:57:20 +0100 Subject: [PATCH] vm_eval.c: loop now yields a incremented counter * vm_eval.c (loop_i): make loop yield a counter to keep track of the iteration count. --- ChangeLog | 5 +++++ test/ruby/test_enumerator.rb | 9 +++++++++ vm_eval.c | 11 +++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 10036a4..af111f8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Nov 12 05:57:20 2014 Franck Verrot + + * vm_eval.c (loop_i): make loop yield a counter to + keep track of the iteration count. + Wed Nov 12 00:26:37 2014 Tanaka Akira * test/ruby/test_object.rb: Specify an exception class for rescue clause. diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index f2a7afa..e86803b 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -47,6 +47,15 @@ class TestEnumerator < Test::Unit::TestCase } end + def test_loop_yield + max = 0 + loop do |i| + max = i + raise StopIteration if i >= 1 + end + assert_equal 1, max + end + def test_nested_iteration def (o = Object.new).each yield :ok1 diff --git a/vm_eval.c b/vm_eval.c index a057ece..f376594 100644 --- a/vm_eval.c +++ b/vm_eval.c @@ -1007,10 +1007,13 @@ rb_yield_block(VALUE val, VALUE arg, int argc, const VALUE *argv, VALUE blockarg static VALUE loop_i(void) { - for (;;) { - rb_yield_0(0, 0); - } - return Qnil; + VALUE counter = INT2FIX(0); + VALUE increment = INT2FIX(1); + for (;;) { + rb_yield_values(1, counter); + counter = rb_funcall(counter, '+', 1, increment); + } + return Qnil; } static VALUE -- 1.8.5.2 (Apple Git-48)