From ded27bf5dc25d61c1483ae2a79570b1435c8348b Mon Sep 17 00:00:00 2001 From: shugo Date: Mon, 19 Mar 2012 08:22:29 +0000 Subject: * 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 --- ChangeLog | 8 +++++++- enumerator.c | 32 +++++++++++++++++++++++++++++++- test/ruby/test_lazy_enumerator.rb | 21 +++++++++++++++++++++ 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 + + * 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 - * 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 diff --git a/enumerator.c b/enumerator.c index 9484f495cc..865730912f 100644 --- a/enumerator.c +++ b/enumerator.c @@ -1299,6 +1299,32 @@ lazy_flat_map_i(VALUE i, VALUE yielder, int argc, VALUE *argv) return rb_funcall2(yielder, id_yield, argc, 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) { @@ -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) -- cgit v1.2.3