diff --git a/ChangeLog b/ChangeLog index 56b9a48872..09c310310c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Wed Apr 15 20:24:49 2009 Yusuke Endoh + + * array.c (rb_ary_flatten): flatten(0) works as Array#dup. + [ruby-core:23168] + + * test/ruby/test_array.rb: add a test for above. + Wed Apr 15 11:53:35 2009 NARUSE, Yui * dir.c (fnmatch_helper): use rb_enc_precise_mbclen and diff --git a/array.c b/array.c index b5f1bcf93b..6da6ef9639 100644 --- a/array.c +++ b/array.c @@ -648,6 +648,12 @@ ary_make_partial(VALUE ary, VALUE klass, long offset, long len) } } +static VALUE +ary_make_shared_copy(VALUE ary) +{ + return ary_make_partial(ary, rb_obj_class(ary), 0, RARRAY_LEN(ary)); +} + enum ary_take_pos_flags { ARY_TAKE_FIRST = 0, @@ -3360,7 +3366,7 @@ rb_ary_flatten(int argc, VALUE *argv, VALUE ary) rb_scan_args(argc, argv, "01", &lv); if (!NIL_P(lv)) level = NUM2INT(lv); - if (level == 0) return ary; + if (level == 0) return ary_make_shared_copy(ary); result = flatten(ary, level, &mod); OBJ_INFECT(result, ary); diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 92c309241f..9b3dfdf45f 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -719,6 +719,11 @@ class TestArray < Test::Unit::TestCase a7 = a6.flatten assert_equal(true, a7.tainted?) assert_equal(true, a7.untrusted?) + + a8 = @cls[[1, 2], 3] + a9 = a8.flatten(0) + assert_equal(a8, a9) + assert_not_same(a8, a9) end def test_flatten!