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

ruby-changes:26952

From: charliesome <ko1@a...>
Date: Fri, 1 Feb 2013 23:06:07 +0900 (JST)
Subject: [ruby-changes:26952] charliesome:r39004 (trunk): * array.c (rb_ary_dup): make returned array the same class as the original

charliesome	2013-02-01 23:05:49 +0900 (Fri, 01 Feb 2013)

  New Revision: 39004

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

  Log:
    * array.c (rb_ary_dup): make returned array the same class as the original
      array [Bug #7768] [ruby-core:51792]
    * test/ruby/test_array.rb (class TestArray): add test

  Modified files:
    trunk/ChangeLog
    trunk/array.c
    trunk/test/ruby/test_array.rb

Index: array.c
===================================================================
--- array.c	(revision 39003)
+++ array.c	(revision 39004)
@@ -1778,7 +1778,8 @@ rb_ary_empty_p(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L1778
 VALUE
 rb_ary_dup(VALUE ary)
 {
-    VALUE dup = rb_ary_new2(RARRAY_LEN(ary));
+    VALUE ary_class = rb_obj_class(ary);
+    VALUE dup = ary_new(ary_class ? ary_class : rb_cArray, RARRAY_LEN(ary));
     MEMCPY(RARRAY_PTR(dup), RARRAY_PTR(ary), VALUE, RARRAY_LEN(ary));
     ARY_SET_LEN(dup, RARRAY_LEN(ary));
     return dup;
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 39003)
+++ ChangeLog	(revision 39004)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Feb  1 23:04:00 2013  Charlie Somerville  <charlie@c...>
+
+	* array.c (rb_ary_dup): make returned array the same class as the original
+	  array [Bug #7768] [ruby-core:51792]
+	* test/ruby/test_array.rb (class TestArray): add test
+
 Fri Feb  1 16:35:34 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* marshal.c (r_object0): prohibit setting instance variables of
Index: test/ruby/test_array.rb
===================================================================
--- test/ruby/test_array.rb	(revision 39003)
+++ test/ruby/test_array.rb	(revision 39004)
@@ -2283,4 +2283,11 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2283
 
     assert_include([4, 7], a.bsearch {|x| (2**100).coerce((1 - x / 4) * (2**100)).first })
   end
+
+  def test_array_uniq_returns_same_class # [ruby-core:51792]
+    klass = Class.new(Array)
+    assert_equal klass, klass.new(2).uniq.class
+    assert_equal klass, klass.new(1).uniq.class
+    assert_equal klass, klass.new(0).uniq.class
+  end
 end

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

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