aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorshugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-19 08:22:29 +0000
committershugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-19 08:22:29 +0000
commitded27bf5dc25d61c1483ae2a79570b1435c8348b (patch)
tree8925df9dc23539fca33ca0762d33ace6e6d921d4
parent592a629bd0185060101f14db80fa1d5552380031 (diff)
downloadruby-ded27bf5dc25d61c1483ae2a79570b1435c8348b.tar.gz
* enumerator.c (lazy_flat_map_func): convert the block value to
Array if it doesn't respond to each. [ruby-core:43334] [Bug #6155] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35092 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--enumerator.c32
-rw-r--r--test/ruby/test_lazy_enumerator.rb21
3 files changed, 59 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index a95c72a73a..2101890a94 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,12 @@
+Mon Mar 19 17:18:51 2012 Shugo Maeda <shugo@ruby-lang.org>
+
+ * 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@ruby-lang.org>
- * 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@ruby-lang.org>
diff --git a/enumerator.c b/enumerator.c
index 9484f495cc..865730912f 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -1300,6 +1300,32 @@ lazy_flat_map_i(VALUE i, VALUE yielder, int argc, VALUE *argv)
}
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 @@ lazy_flat_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
}
}
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;
}
diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb
index 0f2fa2bc41..5260043d43 100644
--- a/test/ruby/test_lazy_enumerator.rb
+++ b/test/ruby/test_lazy_enumerator.rb
@@ -110,6 +110,27 @@ class TestLazyEnumerator < Test::Unit::TestCase
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)