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

ruby-changes:35003

From: knu <ko1@a...>
Date: Wed, 6 Aug 2014 20:28:28 +0900 (JST)
Subject: [ruby-changes:35003] knu:r47085 (trunk): * lib/set.rb (Set#replace): Check if an object given is enumerable

knu	2014-08-06 20:28:21 +0900 (Wed, 06 Aug 2014)

  New Revision: 47085

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

  Log:
    * lib/set.rb (Set#replace): Check if an object given is enumerable
      before clearing self.  Reported by yui-knk. [GH-675]
      https://github.com/ruby/ruby/pull/675

  Modified files:
    trunk/ChangeLog
    trunk/lib/set.rb
    trunk/test/test_set.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47084)
+++ ChangeLog	(revision 47085)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Aug  6 20:25:47 2014  Akinori MUSHA  <knu@i...>
+
+	* lib/set.rb (Set#replace): Check if an object given is enumerable
+	  before clearing self.  Reported by yui-knk. [GH-675]
+	  https://github.com/ruby/ruby/pull/675
+
 Wed Aug  6 20:07:26 2014  Masaki Suketa <masaki.suketa@n...>
 
 	* ext/win32ole/win32ole.c (olerecord_ivar_set): remove rb_str_subseq.
Index: lib/set.rb
===================================================================
--- lib/set.rb	(revision 47084)
+++ lib/set.rb	(revision 47085)
@@ -91,9 +91,9 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L91
 
   def do_with_enum(enum, &block) # :nodoc:
     if enum.respond_to?(:each_entry)
-      enum.each_entry(&block)
+      enum.each_entry(&block) if block
     elsif enum.respond_to?(:each)
-      enum.each(&block)
+      enum.each(&block) if block
     else
       raise ArgumentError, "value must be enumerable"
     end
@@ -149,12 +149,12 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L149
   def replace(enum)
     if enum.instance_of?(self.class)
       @hash.replace(enum.instance_variable_get(:@hash))
+      self
     else
+      do_with_enum(enum)
       clear
       merge(enum)
     end
-
-    self
   end
 
   # Converts the set to an array.  The order of elements is uncertain.
Index: test/test_set.rb
===================================================================
--- test/test_set.rb	(revision 47084)
+++ test/test_set.rb	(revision 47085)
@@ -98,6 +98,12 @@ class TC_Set < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L98
 
     assert_same(set, ret)
     assert_equal(Set['a','b','c'], set)
+
+    set = Set[1,2]
+    assert_raise(ArgumentError) {
+      set.replace(3)
+    }
+    assert_equal(Set[1,2], set)
   end
 
   def test_to_a

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

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