mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/prime.rb: Have with_index accept an offset parameter.
Based on patch by T Yamada. [#11007] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50854 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7b91efd7f3
commit
dd8f914d3b
3 changed files with 25 additions and 1 deletions
|
@ -1,3 +1,8 @@
|
|||
Sat Jun 13 00:44:59 2015 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
||||
|
||||
* 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@mbf.nifty.com>
|
||||
|
||||
* test/ruby/test_extlibs.rb (TestExtLibs::check_existence): fix
|
||||
|
|
10
lib/prime.rb
10
lib/prime.rb
|
@ -269,7 +269,15 @@ class Prime
|
|||
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)
|
||||
|
|
|
@ -86,6 +86,17 @@ class TestPrime < Test::Unit::TestCase
|
|||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue