Browse files

Return sized enumerator from Enumerable#index_by

  • Loading branch information...
1 parent 13d2696 commit a476020567a47f5fbec3629707d5bf31b400a284 @marcandre marcandre committed Feb 4, 2014
View
7 activerecord/CHANGELOG.md
@@ -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.
View
2 activesupport/lib/active_support/core_ext/enumerable.rb
@@ -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
View
4 activesupport/test/core_ext/enumerable_test.rb
@@ -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

0 comments on commit a476020

Please sign in to comment.