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

ruby-changes:38776

From: marcandre <ko1@a...>
Date: Sat, 13 Jun 2015 00:46:08 +0900 (JST)
Subject: [ruby-changes:38776] marcandRe: r50857 (trunk): * lib/prime.rb: Return sized enumerators.

marcandre	2015-06-13 00:45:59 +0900 (Sat, 13 Jun 2015)

  New Revision: 50857

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

  Log:
    * lib/prime.rb: Return sized enumerators.
      Patch by Kenichi Kamiya [GH-931]

  Modified files:
    trunk/ChangeLog
    trunk/lib/prime.rb
    trunk/test/test_prime.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50856)
+++ ChangeLog	(revision 50857)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Jun 13 00:45:08 2015  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* lib/prime.rb: Return sized enumerators.
+	  Patch by Kenichi Kamiya [GH-931]
+
 Sat Jun 13 00:45:06 2015  Marc-Andre Lafortune  <ruby-core@m...>
 
 	* lib/prime.rb: Fix with_object with no block given
Index: lib/prime.rb
===================================================================
--- lib/prime.rb	(revision 50856)
+++ lib/prime.rb	(revision 50857)
@@ -270,7 +270,7 @@ class Prime https://github.com/ruby/ruby/blob/trunk/lib/prime.rb#L270
 
     # see +Enumerator+#with_index.
     def with_index(offset = 0)
-      return enum_for(:with_index, offset) unless block_given?
+      return enum_for(:with_index, offset) { Float::INFINITY } unless block_given?
       return each_with_index(&proc) if offset == 0
 
       each do |prime|
@@ -281,11 +281,15 @@ class Prime https://github.com/ruby/ruby/blob/trunk/lib/prime.rb#L281
 
     # see +Enumerator+#with_object.
     def with_object(obj)
-      return enum_for(:with_object, obj) unless block_given?
+      return enum_for(:with_object, obj) { Float::INFINITY } unless block_given?
       each do |prime|
         yield prime, obj
       end
     end
+
+    def size
+      Float::INFINITY
+    end
   end
 
   # An implementation of +PseudoPrimeGenerator+.
Index: test/test_prime.rb
===================================================================
--- test/test_prime.rb	(revision 50856)
+++ test/test_prime.rb	(revision 50857)
@@ -106,6 +106,13 @@ class TestPrime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_prime.rb#L106
     end
   end
 
+  def test_enumerator_size
+    enum = Prime.each
+    assert_equal Float::INFINITY, enum.size
+    assert_equal Float::INFINITY, enum.with_object(nil).size
+    assert_equal Float::INFINITY, enum.with_index(42).size
+  end
+
   def test_default_instance_does_not_have_compatibility_methods
     assert !Prime.instance.respond_to?(:succ)
     assert !Prime.instance.respond_to?(:next)

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

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