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

ruby-changes:14787

From: knu <ko1@a...>
Date: Fri, 12 Feb 2010 02:38:25 +0900 (JST)
Subject: [ruby-changes:14787] Ruby:r26648 (trunk): Fri Feb 12 02:27:39 2010 Akinori MUSHA <knu@i...>

knu	2010-02-12 02:38:05 +0900 (Fri, 12 Feb 2010)

  New Revision: 26648

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

  Log:
    Fri Feb 12 02:27:39 2010  Akinori MUSHA  <knu@i...>
    
    * lib/set.rb (Set#initialize, Set#replace, Set#merge)
      (Set#subtract, Set#&): Fix duck type tests. [ruby-core:28078]
    
    * lib/set.rb (Set#initialize, Set#replace, Set#merge)
      (Set#subtract, Set#&): Try #each if #each_entry fails.

  Modified files:
    trunk/ChangeLog
    trunk/lib/set.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 26647)
+++ ChangeLog	(revision 26648)
@@ -1,3 +1,11 @@
+Fri Feb 12 02:27:39 2010  Akinori MUSHA  <knu@i...>
+
+	* lib/set.rb (Set#initialize, Set#replace, Set#merge)
+	  (Set#subtract, Set#&): Fix duck type tests. [ruby-core:28078]
+
+	* lib/set.rb (Set#initialize, Set#replace, Set#merge)
+	  (Set#subtract, Set#&): Try #each if #each_entry fails.
+
 Thu Feb 11 20:43:00 2010  Tanaka Akira  <akr@f...>
 
 	* io.c (rb_io_oflags_modestr): return "r" for O_RDONLY|O_APPEND.
@@ -358,6 +366,7 @@
 	  for duck typing.
 
 	* lib/set.rb (SortedSet#add): typo fixed.
+
 Tue Feb  2 11:13:56 2010  Nobuyoshi Nakada  <nobu@r...>
 
 	* lib/delegate.rb (Delegator#marshal_dump): exclude
Index: lib/set.rb
===================================================================
--- lib/set.rb	(revision 26647)
+++ lib/set.rb	(revision 26648)
@@ -70,13 +70,23 @@
     enum.nil? and return
 
     if block
-      enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
-      enum.each_entry { |o| add(block[o]) }
+      do_with_enum(enum) { |o| add(block[o]) }
     else
       merge(enum)
     end
   end
 
+  def do_with_enum(enum, &block)
+    if enum.respond_to?(:each_entry)
+      enum.each_entry(&block)
+    elsif enum.respond_to?(:each)
+      enum.each(&block)
+    else
+      raise ArgumentError, "value must be enumerable"
+    end
+  end
+  private :do_with_enum
+
   # Copy internal hash.
   def initialize_copy(orig)
     @hash = orig.instance_eval{@hash}.dup
@@ -123,9 +133,8 @@
     if enum.class == self.class
       @hash.replace(enum.instance_eval { @hash })
     else
-      enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
       clear
-      enum.each_entry { |o| add(o) }
+      merge(enum)
     end
 
     self
@@ -281,8 +290,7 @@
     if enum.instance_of?(self.class)
       @hash.update(enum.instance_variable_get(:@hash))
     else
-      enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
-      enum.each_entry { |o| add(o) }
+      do_with_enum(enum) { |o| add(o) }
     end
 
     self
@@ -291,8 +299,7 @@
   # Deletes every element that appears in the given enumerable object
   # and returns self.
   def subtract(enum)
-    enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
-    enum.each_entry { |o| delete(o) }
+    do_with_enum(enum) { |o| delete(o) }
     self
   end
 
@@ -314,9 +321,8 @@
   # Returns a new set containing elements common to the set and the
   # given enumerable object.
   def &(enum)
-    enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
     n = self.class.new
-    enum.each_entry { |o| n.add(o) if include?(o) }
+    do_with_enum(enum) { |o| n.add(o) if include?(o) }
     n
   end
   alias intersection &	##

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

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