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

ruby-changes:23042

From: shugo <ko1@a...>
Date: Mon, 19 Mar 2012 17:22:40 +0900 (JST)
Subject: [ruby-changes:23042] shugo:r35092 (trunk): * enumerator.c (lazy_flat_map_func): convert the block value to

shugo	2012-03-19 17:22:29 +0900 (Mon, 19 Mar 2012)

  New Revision: 35092

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

  Log:
    * enumerator.c (lazy_flat_map_func): convert the block value to
      Array if it doesn't respond to each.  [ruby-core:43334]
      [Bug #6155]

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 35091)
+++ ChangeLog	(revision 35092)
@@ -1,6 +1,12 @@
+Mon Mar 19 17:18:51 2012  Shugo Maeda  <shugo@r...>
+
+	* enumerator.c (lazy_flat_map_func): convert the block value to
+	  Array if it doesn't respond to each.  [ruby-core:43334]
+	  [Bug #6155]
+
 Mon Mar 19 16:34:14 2012  Shugo Maeda  <shugo@r...>
 
-	* enum.c (zip_i):  variadic argument needs explicit cast on the
+	* enum.c (zip_i): variadic argument needs explicit cast on the
 	  platforms where VALUE is longer than int.
 
 Mon Mar 19 15:36:41 2012  Shugo Maeda  <shugo@r...>
Index: enumerator.c
===================================================================
--- enumerator.c	(revision 35091)
+++ enumerator.c	(revision 35092)
@@ -1300,6 +1300,32 @@
 }
 
 static VALUE
+lazy_flat_map_each(VALUE obj)
+{
+    NODE *memo = RNODE(obj);
+    rb_block_call(memo->u1.value, id_each, 0, 0, lazy_flat_map_i,
+		  memo->u2.value);
+    return Qnil;
+}
+
+static VALUE
+lazy_flat_map_to_ary(VALUE obj)
+{
+    NODE *memo = RNODE(obj);
+    VALUE ary = rb_check_array_type(memo->u1.value);
+    if (NIL_P(ary)) {
+	rb_funcall(memo->u2.value, id_yield, 1, memo->u1.value);
+    }
+    else {
+	long i;
+	for (i = 0; i < RARRAY_LEN(ary); i++) {
+	    rb_funcall(memo->u2.value, id_yield, 1, RARRAY_PTR(ary)[i]);
+	}
+    }
+    return Qnil;
+}
+
+static VALUE
 lazy_flat_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
 {
     VALUE result = rb_yield_values2(argc - 1, &argv[1]);
@@ -1310,7 +1336,11 @@
 	}
     }
     else {
-	rb_block_call(result, id_each, 0, 0, lazy_flat_map_i, argv[0]);
+	NODE *memo;
+	memo = NEW_MEMO(result, argv[0], 0);
+	rb_rescue2(lazy_flat_map_each, (VALUE) memo,
+		   lazy_flat_map_to_ary, (VALUE) memo,
+		   rb_eNoMethodError, (VALUE)0);
     }
     return Qnil;
 }
Index: test/ruby/test_lazy_enumerator.rb
===================================================================
--- test/ruby/test_lazy_enumerator.rb	(revision 35091)
+++ test/ruby/test_lazy_enumerator.rb	(revision 35092)
@@ -110,6 +110,27 @@
     assert_equal(1, a.current)
   end
 
+  def test_flat_map_to_ary
+    to_ary = Class.new {
+      def initialize(value)
+        @value = value
+      end
+
+      def to_ary
+        [:to_ary, @value]
+      end
+    }
+    assert_equal([:to_ary, 1, :to_ary, 2, :to_ary, 3],
+                 [1, 2, 3].flat_map {|x| to_ary.new(x)})
+    assert_equal([:to_ary, 1, :to_ary, 2, :to_ary, 3],
+                 [1, 2, 3].lazy.flat_map {|x| to_ary.new(x)}.force)
+  end
+
+  def test_flat_map_non_array
+    assert_equal(["1", "2", "3"], [1, 2, 3].flat_map {|x| x.to_s})
+    assert_equal(["1", "2", "3"], [1, 2, 3].lazy.flat_map {|x| x.to_s}.force)
+  end
+
   def test_reject
     a = Step.new(1..6)
     assert_equal(4, a.reject {|x| x < 4}.first)

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

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