mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
592a629bd0
commit
ded27bf5dc
3 changed files with 59 additions and 2 deletions
|
@ -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>
|
||||
|
|
32
enumerator.c
32
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;
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue