From d4e16d760ad84cfb16f047868ffd1a43eb91f3e4 Mon Sep 17 00:00:00 2001 From: Adam Keys Date: Wed, 3 Sep 2008 05:17:15 -0500 Subject: [PATCH] Add injecting to enumerable --- .../lib/active_support/core_ext/enumerable.rb | 25 +++++++++++++++++++- activesupport/test/core_ext/enumerable_test.rb | 5 ++++ 2 files changed, 29 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index e451e99..61a9d4e 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -63,7 +63,30 @@ module Enumerable inject { |sum, element| sum + element } end end - + + # Syntactic sugar for inject, doesn't require you to return a value from the block parameter + # + # Injecting is just sugar on top of Ruby's inject. The sugar is in + # using the same accumulator reference every time the block is executed, + # rather than requiring you to return the accumulator at the end of your + # block. + # + # With Ruby's standard inject, this code would fail in a peculiar way: + # + # %w(foo bar).injecting({}) { |hsh, str| hsh[str] = str.upcase } + # + # However with injecting, we'll get something useful: + # + # {'foo' => 'FOO', 'bar' => 'BAR'} + # + def injecting(accumulator, &block) + inject(accumulator) do |accumulator, element| + returning accumulator do |accumulator| + block.call(accumulator, element) + end + end + end + # Convert an enumerable to a hash. Examples: # # people.index_by(&:login) diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 2315d8f..2395ada 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -58,6 +58,11 @@ class EnumerableTests < Test::Unit::TestCase assert_equal Payment.new(0), [].sum(Payment.new(0)) end + def test_injecting + result = %w(foo bar).injecting({}) { |hsh, str| 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] }, -- 1.5.5.3