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

ruby-changes:35001

From: knu <ko1@a...>
Date: Wed, 6 Aug 2014 19:13:41 +0900 (JST)
Subject: [ruby-changes:35001] knu:r47083 (trunk): Implement Set#clone. [Fixes GH-661]

knu	2014-08-06 19:13:34 +0900 (Wed, 06 Aug 2014)

  New Revision: 47083

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

  Log:
    Implement Set#clone. [Fixes GH-661]

  Modified files:
    trunk/ChangeLog
    trunk/lib/set.rb
    trunk/test/test_set.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47082)
+++ ChangeLog	(revision 47083)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Aug  6 19:09:27 2014  Akinori MUSHA  <knu@i...>
+
+	* lib/set.rb (Set): Implement Set#clone by splitting
+	  initialize_copy into initialize_dup and initialize_clone.
+	  Submitted by yui-knk. [Fixes GH-661]
+	  https://github.com/ruby/ruby/pull/661
+
 Wed Aug  6 18:42:58 2014  Masaki Suketa <masaki.suketa@n...>
 
 	* ext/win32ole/win32ole.c: separate src of WIN32OLERuntimeError
Index: lib/set.rb
===================================================================
--- lib/set.rb	(revision 47082)
+++ lib/set.rb	(revision 47083)
@@ -100,11 +100,18 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L100
   end
   private :do_with_enum
 
-  # Copy internal hash.
-  def initialize_copy(orig)
+  # Dup internal hash.
+  def initialize_dup(orig)
+    super
     @hash = orig.instance_variable_get(:@hash).dup
   end
 
+  # Clone internal hash.
+  def initialize_clone(orig)
+    super
+    @hash = orig.instance_variable_get(:@hash).clone
+  end
+
   def freeze    # :nodoc:
     @hash.freeze
     super
Index: test/test_set.rb
===================================================================
--- test/test_set.rb	(revision 47082)
+++ test/test_set.rb	(revision 47083)
@@ -585,6 +585,28 @@ class TC_Set < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L585
     assert_equal 4, set.size
   end
 
+  def test_freeze_dup
+    set1 = Set[1,2,3]
+    set1.freeze
+    set2 = set1.dup
+
+    assert_not_predicate set2, :frozen?
+    assert_nothing_raised {
+      set2.add 4
+    }
+  end
+
+  def test_freeze_clone
+    set1 = Set[1,2,3]
+    set1.freeze
+    set2 = set1.clone
+
+    assert_predicate set2, :frozen?
+    assert_raise(RuntimeError) {
+      set2.add 5
+    }
+  end
+
   def test_inspect
     set1 = Set[1]
 

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

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