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

ruby-changes:38827

From: knu <ko1@a...>
Date: Mon, 15 Jun 2015 14:38:01 +0900 (JST)
Subject: [ruby-changes:38827] knu:r50908 (trunk): * lib/set.rb: Make Set#each and SortedSet#each generate a sized

knu	2015-06-15 14:37:38 +0900 (Mon, 15 Jun 2015)

  New Revision: 50908

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

  Log:
    * lib/set.rb: Make Set#each and SortedSet#each generate a sized
      enumerator. [GH-931] by kachick (Kenichi Kamiya)
    
    * test/test_set.rb: Import tests from Set into SortedSet. [GH-931]
      by kachick (Kenichi Kamiya)

  Modified files:
    trunk/ChangeLog
    trunk/lib/set.rb
    trunk/test/test_set.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50907)
+++ ChangeLog	(revision 50908)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Jun 15 14:33:02 2015  Akinori MUSHA  <knu@i...>
+
+	* lib/set.rb: Make Set#each and SortedSet#each generate a sized
+	  enumerator. [GH-931] by kachick (Kenichi Kamiya)
+
+	* test/test_set.rb: Import tests from Set into SortedSet. [GH-931]
+	  by kachick (Kenichi Kamiya)
+
 Mon Jun 15 02:26:34 2015  NARUSE, Yui  <naruse@r...>
 
 	* lib/net/http.rb (Net::HTTP#connect): use connect_nonblock and
Index: lib/set.rb
===================================================================
--- lib/set.rb	(revision 50907)
+++ lib/set.rb	(revision 50908)
@@ -284,7 +284,7 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L284
   # the element as parameter.  Returns an enumerator if no block is
   # given.
   def each(&block)
-    block or return enum_for(__method__)
+    block or return enum_for(__method__) { size }
     @hash.each_key(&block)
     self
   end
@@ -663,7 +663,7 @@ class SortedSet < Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L663
           end
 
           def each(&block)
-            block or return enum_for(__method__)
+            block or return enum_for(__method__) { size }
             to_a.each(&block)
             self
           end
Index: test/test_set.rb
===================================================================
--- test/test_set.rb	(revision 50907)
+++ test/test_set.rb	(revision 50908)
@@ -364,6 +364,10 @@ class TC_Set < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L364
 
       ary.empty? or raise "forgotten elements: #{ary.join(', ')}"
     }
+
+    assert_equal(6, e.size)
+    set << 42
+    assert_equal(7, e.size)
   end
 
   def test_add
@@ -669,6 +673,29 @@ class TC_SortedSet < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L673
     assert_equal(['four', 'one', 'three', 'two'], s.to_a)
     assert_equal(['four', 'one', 'three', 'two'], a)
   end
+
+  def test_each
+    ary = [1,3,5,7,10,20]
+    set = SortedSet.new(ary)
+
+    ret = set.each { |o| }
+    assert_same(set, ret)
+
+    e = set.each
+    assert_instance_of(Enumerator, e)
+
+    assert_nothing_raised {
+      set.each { |o|
+        ary.delete(o) or raise "unexpected element: #{o}"
+      }
+
+      ary.empty? or raise "forgotten elements: #{ary.join(', ')}"
+    }
+
+    assert_equal(6, e.size)
+    set << 42
+    assert_equal(7, e.size)
+  end
 end
 
 class TC_Enumerable < Test::Unit::TestCase

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

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