[前][次][番号順一覧][スレッド一覧]

ruby-changes:22993

From: shugo <ko1@a...>
Date: Thu, 15 Mar 2012 19:14:36 +0900 (JST)
Subject: [ruby-changes:22993] shugo:r35042 (trunk): * enumerator.c (lazy_zip): rescue StopIteration returned by

shugo	2012-03-15 19:14:22 +0900 (Thu, 15 Mar 2012)

  New Revision: 35042

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35042

  Log:
    * enumerator.c (lazy_zip): rescue StopIteration returned by
      Enumerator#next.

  Modified files:
    trunk/ChangeLog
    trunk/enumerator.c
    trunk/test/ruby/test_lazy_enumerator.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 35041)
+++ ChangeLog	(revision 35042)
@@ -1,3 +1,8 @@
+Thu Mar 15 19:12:31 2012  Shugo Maeda  <shugo@r...>
+
+	* enumerator.c (lazy_zip): rescue StopIteration returned by
+	  Enumerator#next.
+
 Thu Mar 15 18:19:53 2012  Shugo Maeda  <shugo@r...>
 
 	* enumerator.c (lazy_zip, lazy_cycle): Enumerator::Lazy#{zip,cycle}
Index: enumerator.c
===================================================================
--- enumerator.c	(revision 35041)
+++ enumerator.c	(revision 35042)
@@ -1374,6 +1374,18 @@
 }
 
 static VALUE
+call_next(VALUE obj)
+{
+    return rb_funcall(obj, id_next, 0);
+}
+
+static VALUE
+next_stopped(VALUE obj)
+{
+    return Qnil;
+}
+
+static VALUE
 lazy_zip_func(VALUE val, VALUE arg, int argc, VALUE *argv)
 {
     VALUE yielder, ary, v;
@@ -1383,7 +1395,8 @@
     ary = rb_ary_new2(RARRAY_LEN(arg) + 1);
     rb_ary_push(ary, argv[1]);
     for (i = 0; i < RARRAY_LEN(arg); i++) {
-	v = rb_funcall(RARRAY_PTR(arg)[i], id_next, 0);
+	v = rb_rescue2(call_next, RARRAY_PTR(arg)[i], next_stopped, 0,
+		       rb_eStopIteration, 0);
 	rb_ary_push(ary, v);
     }
     rb_funcall(yielder, id_yield, 1, ary);
Index: test/ruby/test_lazy_enumerator.rb
===================================================================
--- test/ruby/test_lazy_enumerator.rb	(revision 35041)
+++ test/ruby/test_lazy_enumerator.rb	(revision 35042)
@@ -130,6 +130,12 @@
     assert_equal(1, a.current)
   end
 
+  def test_zip_short_arg
+    a = Step.new(1..5)
+    assert_equal([5, nil], a.zip("a".."c").last)
+    assert_equal([5, nil], a.lazy.zip("a".."c").force.last)
+  end
+
   def test_zip_without_arg
     a = Step.new(1..3)
     assert_equal([1], a.zip.first)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]