mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add each_with_object from 1.9 for a more convenient alternative to inject.
Signed-off-by: Michael Koziarski <michael@koziarski.com> [#962 state:committed]
This commit is contained in:
parent
b36d000975
commit
10fe6a6d89
2 changed files with 26 additions and 1 deletions
|
@ -64,8 +64,28 @@ module Enumerable
|
|||
end
|
||||
end
|
||||
|
||||
# Iterates over a collection, passing the current element *and* the
|
||||
# +memo+ to the block. Handy for building up hashes or
|
||||
# reducing collections down to one object. Examples:
|
||||
#
|
||||
# %w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase } #=> {'foo' => 'FOO', 'bar' => 'BAR'}
|
||||
#
|
||||
# *Note* that you can't use immutable objects like numbers, true or false as
|
||||
# the memo. You would think the following returns 120, but since the memo is
|
||||
# never changed, it does not.
|
||||
#
|
||||
# (1..5).each_with_object(1) { |value, memo| memo *= value } # => 1
|
||||
#
|
||||
def each_with_object(memo, &block)
|
||||
returning memo do |memo|
|
||||
each do |element|
|
||||
block.call(element, memo)
|
||||
end
|
||||
end
|
||||
end unless [].respond_to?(:each_with_object)
|
||||
|
||||
# Convert an enumerable to a hash. Examples:
|
||||
#
|
||||
#
|
||||
# people.index_by(&:login)
|
||||
# => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
|
||||
# people.index_by { |person| "#{person.first_name} #{person.last_name}" }
|
||||
|
|
|
@ -58,6 +58,11 @@ class EnumerableTests < Test::Unit::TestCase
|
|||
assert_equal Payment.new(0), [].sum(Payment.new(0))
|
||||
end
|
||||
|
||||
def test_each_with_object
|
||||
result = %w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase }
|
||||
assert_equal({'foo' => 'FOO', 'bar' => 'BAR'}, result)
|
||||
end
|
||||
|
||||
def test_index_by
|
||||
payments = [ Payment.new(5), Payment.new(15), Payment.new(10) ]
|
||||
assert_equal({ 5 => payments[0], 15 => payments[1], 10 => payments[2] },
|
||||
|
|
Loading…
Reference in a new issue