提交 a4760205 编写于 作者: M Marc-Andre Lafortune

Return sized enumerator from Enumerable#index_by

上级 13d2696c
<<<<<<< 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 @@ def index_by
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 @@ def test_index_by
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册