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

ruby-changes:38773

From: marcandre <ko1@a...>
Date: Sat, 13 Jun 2015 00:45:43 +0900 (JST)
Subject: [ruby-changes:38773] marcandRe: r50854 (trunk): * lib/prime.rb: Have with_index accept an offset parameter.

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

  New Revision: 50854

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

  Log:
    * lib/prime.rb: Have with_index accept an offset parameter.
      Based on patch by T Yamada. [#11007]

  Modified files:
    trunk/ChangeLog
    trunk/lib/prime.rb
    trunk/test/test_prime.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50853)
+++ ChangeLog	(revision 50854)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Jun 13 00:44:59 2015  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* lib/prime.rb: Have with_index accept an offset parameter.
+	  Based on patch by T Yamada. [#11007]
+
 Fri Jun 12 22:21:12 2015  Kazuhiro NISHIYAMA  <zn@m...>
 
 	* test/ruby/test_extlibs.rb (TestExtLibs::check_existence): fix
Index: lib/prime.rb
===================================================================
--- lib/prime.rb	(revision 50853)
+++ lib/prime.rb	(revision 50854)
@@ -269,7 +269,15 @@ class Prime https://github.com/ruby/ruby/blob/trunk/lib/prime.rb#L269
     end
 
     # see +Enumerator+#with_index.
-    alias with_index each_with_index
+    def with_index(offset = 0)
+      return enum_for(:with_index, offset) unless block_given?
+      return each_with_index(&proc) if offset == 0
+
+      each do |prime|
+        yield prime, offset
+        offset += 1
+      end
+    end
 
     # see +Enumerator+#with_object.
     def with_object(obj)
Index: test/test_prime.rb
===================================================================
--- test/test_prime.rb	(revision 50853)
+++ test/test_prime.rb	(revision 50854)
@@ -86,6 +86,17 @@ class TestPrime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_prime.rb#L86
     end
   end
 
+  def test_enumerator_with_index_with_offset
+    enum = Prime.each
+    last = 5-1
+    enum.with_index(5).each do |p,i|
+      break if i >= 100+5
+      assert_equal last+1, i
+      assert_equal PRIMES[i-5], p
+      last = i
+    end
+  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/

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