mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/prime.rb: Return sized enumerators.
Patch by Kenichi Kamiya [GH-931] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50857 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5b78e48c31
commit
b28f3e5c4f
3 changed files with 18 additions and 2 deletions
|
@ -1,3 +1,8 @@
|
|||
Sat Jun 13 00:45:08 2015 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
||||
|
||||
* lib/prime.rb: Return sized enumerators.
|
||||
Patch by Kenichi Kamiya [GH-931]
|
||||
|
||||
Sat Jun 13 00:45:06 2015 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
||||
|
||||
* lib/prime.rb: Fix with_object with no block given
|
||||
|
|
|
@ -270,7 +270,7 @@ class Prime
|
|||
|
||||
# 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
|
|||
|
||||
# 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+.
|
||||
|
|
|
@ -106,6 +106,13 @@ class TestPrime < Test::Unit::TestCase
|
|||
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)
|
||||
|
|
Loading…
Reference in a new issue