Skip to content

Commit c99670d

Browse files
committed
Revert the default size of Enumerator::Producer to infinity
[Bug #21780]
1 parent f45b1e3 commit c99670d

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ Note: We're only listing outstanding class updates.
3838
* `Enumerator.produce` now accepts an optional `size` keyword argument
3939
to specify the size of the enumerator. It can be an integer,
4040
`Float::INFINITY`, a callable object (such as a lambda), or `nil` to
41-
indicate unknown size. When not specified, the size is unknown (`nil`).
42-
Previously, the size was always `Float::INFINITY` and not specifiable.
41+
indicate unknown size. When not specified, the size defaults to
42+
`Float::INFINITY`.
4343
4444
```ruby
4545
# Infinite enumerator

enumerator.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3037,7 +3037,8 @@ producer_size(VALUE obj, VALUE args, VALUE eobj)
30373037
* The optional +size+ keyword argument specifies the size of the enumerator,
30383038
* which can be retrieved by Enumerator#size. It can be an integer,
30393039
* +Float::INFINITY+, a callable object (such as a lambda), or +nil+ to
3040-
* indicate unknown size. When not specified, the size is unknown (+nil+).
3040+
* indicate unknown size. When not specified, the size defaults to
3041+
* +Float::INFINITY+.
30413042
*
30423043
* # Infinite enumerator
30433044
* enum = Enumerator.produce(1, size: Float::INFINITY, &:succ)
@@ -3063,7 +3064,7 @@ enumerator_s_produce(int argc, VALUE *argv, VALUE klass)
30633064
rb_scan_args_kw(RB_SCAN_ARGS_LAST_HASH_KEYWORDS, argc, argv, "01:", &init, &opts);
30643065
rb_get_kwargs(opts, keyword_ids, 0, 1, &size);
30653066

3066-
size = UNDEF_P(size) ? Qnil : convert_to_feasible_size_value(size);
3067+
size = UNDEF_P(size) ? DBL2NUM(HUGE_VAL) : convert_to_feasible_size_value(size);
30673068

30683069
if (argc == 0 || (argc == 1 && !NIL_P(opts))) {
30693070
init = Qundef;

test/ruby/test_enumerator.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -892,22 +892,22 @@ def test_produce
892892
passed_args = []
893893
enum = Enumerator.produce { |obj| passed_args << obj; (obj || 0).succ }
894894
assert_instance_of(Enumerator, enum)
895-
assert_nil enum.size
895+
assert_equal Float::INFINITY, enum.size
896896
assert_equal [1, 2, 3], enum.take(3)
897897
assert_equal [nil, 1, 2], passed_args
898898

899899
# With initial object
900900
passed_args = []
901901
enum = Enumerator.produce(1) { |obj| passed_args << obj; obj.succ }
902902
assert_instance_of(Enumerator, enum)
903-
assert_nil enum.size
903+
assert_equal Float::INFINITY, enum.size
904904
assert_equal [1, 2, 3], enum.take(3)
905905
assert_equal [1, 2], passed_args
906906

907907
# Raising StopIteration
908908
words = "The quick brown fox jumps over the lazy dog.".scan(/\w+/)
909909
enum = Enumerator.produce { words.shift or raise StopIteration }
910-
assert_nil enum.size
910+
assert_equal Float::INFINITY, enum.size
911911
assert_instance_of(Enumerator, enum)
912912
assert_equal %w[The quick brown fox jumps over the lazy dog], enum.to_a
913913

@@ -917,7 +917,7 @@ def test_produce
917917
obj.respond_to?(:first) or raise StopIteration
918918
obj.first
919919
}
920-
assert_nil enum.size
920+
assert_equal Float::INFINITY, enum.size
921921
assert_instance_of(Enumerator, enum)
922922
assert_nothing_raised {
923923
assert_equal [

0 commit comments

Comments
 (0)