mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Return sized enumerator from Enumerable#index_by
This commit is contained in:
parent
13d2696c10
commit
a476020567
3 changed files with 6 additions and 7 deletions
|
@ -1,10 +1,5 @@
|
|||
<<<<<<< HEAD
|
||||
* `find_in_batches`, `find_each` now
|
||||
* `find_in_batches`, `find_each`, `Result#each` and `Enumerable#index_by` now
|
||||
return an `Enumerator` that can calculate its size.
|
||||
=======
|
||||
* `find_in_batches`, `find_each`, `Result#each` now returns an `Enumerator`
|
||||
that can calculate its size.
|
||||
>>>>>>> 5863938... Return sized enumerator from Result#each
|
||||
|
||||
See also #13938.
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ module Enumerable
|
|||
if block_given?
|
||||
Hash[map { |elem| [yield(elem), elem] }]
|
||||
else
|
||||
to_enum(:index_by) { size }
|
||||
to_enum(:index_by) { size if respond_to?(:size) }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -73,6 +73,10 @@ class EnumerableTests < ActiveSupport::TestCase
|
|||
assert_equal({ 5 => Payment.new(5), 15 => Payment.new(15), 10 => Payment.new(10) },
|
||||
payments.index_by { |p| p.price })
|
||||
assert_equal Enumerator, payments.index_by.class
|
||||
if Enumerator.method_defined? :size
|
||||
assert_equal nil, payments.index_by.size
|
||||
assert_equal 42, (1..42).index_by.size
|
||||
end
|
||||
assert_equal({ 5 => Payment.new(5), 15 => Payment.new(15), 10 => Payment.new(10) },
|
||||
payments.index_by.each { |p| p.price })
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue