Enumerable#inject

enum.inject(initial) { |acc, elem| }
Returns: Object · Updated March 15, 2026 · Enumerable
ruby enumerable

The inject method (also known as reduce or fold in other programming languages) is one of the most powerful methods in Ruby Enumerable module. It accumulates values by applying a binary operation to each element, carrying the result forward until the entire collection is reduced to a single value.

This method is essential for tasks like summing numbers, building strings, finding maximum values, or any operation that combines all elements into one result.

Basic Usage

The simplest form of inject accumulates all elements using a starting value:

numbers = [1, 2, 3, 4, 5]
sum = numbers.inject(0) { |acc, num| acc + num }
# => 15

The block receives two parameters: the accumulator and the current element. The return value of the block becomes the new accumulator for the next iteration.

Shorthand Symbol Syntax

Ruby provides a convenient shorthand when the operation is a simple method call:

numbers = [1, 2, 3, 4, 5]
sum = numbers.inject(:+)
# => 15

This is equivalent to the block version but more concise. You can use any binary method name as a symbol.

Without Initial Value

If you omit the initial value, inject uses the first element as the starting accumulator:

numbers = [1, 2, 3, 4, 5]
sum = numbers.inject { |acc, num| acc + num }
# => 15

This works because Ruby starts with element 1 as the accumulator and then processes elements 2 through 5.

Caution: With an empty collection and no initial value, inject returns nil:

[].inject { |acc, x| acc + x }
# => nil

Common Examples

Summing Numbers

prices = [10, 20, 30, 40]
total = prices.inject(0, :+)
# => 100

Finding Maximum Value

scores = [45, 87, 23, 92, 55]
highest = scores.inject { |max, score| score > max ? score : max }
# => 92

Building a String

words = ["Hello", "World", "from", "Ruby"]
sentence = words.inject("") { |str, word| str + word + " " }
# => "Hello World from Ruby "

Flattening Nested Arrays

matrix = [[1, 2], [3, 4], [5, 6]]
flat = matrix.inject([]) { |acc, arr| acc + arr }
# => [1, 2, 3, 4, 5, 6]

Creating a Hash from Arrays

keys = [:a, :b, :c]
values = [1, 2, 3]
hash = keys.zip(values).inject({}) { |h, (k, v)| h.merge(k => v) }
# => { a: 1, b: 2, c: 3 }

The reduce Alias

reduce is an alias for inject — they behave identically:

[1, 2, 3].reduce(0, :+)
# => 6

[1, 2, 3].inject(0, :+)
# => 6

Many developers prefer reduce because the name more intuitively describes what happens.

Practical Patterns

Factorial Calculation

def factorial(n)
  (1..n).inject(1, :*)
end

factorial(5)
# => 120

Chaining Transformations

result = [1, 2, 3, 4, 5]
  .inject([]) { |acc, x| acc << x * 2 }
  .inject(0, :+)

# => 30

Performance Notes

inject is efficient and lazy in the sense that it does not create intermediate collections when using the symbol shorthand. However, for simple sums, specialized methods like sum may be more readable:

# Using inject
[1, 2, 3].inject(0, :+)

# Using sum (available in Ruby 2.4+)
[1, 2, 3].sum

Return Value

inject returns the final accumulated value. For empty collections:

  • With initial value: returns that initial value
  • Without initial value: returns nil

Edge Cases

# Single element with initial value
[5].inject(10, :+)
# => 15

# Single element without initial value
[5].inject { |acc, x| acc + x }
# => 5

# With strings (using concatenation)
["a", "b", "c"].inject(:+)
# => "abc"

See Also