Summing the Items of an Array


You want to add together many objects in an array.


There are two good ways to accomplish this in Ruby. Plain vanilla iteration is a simple way to approach the problem:

	collection = [1, 2, 3, 4, 5]
	sum = 0 
	collection.each {|i| sum += i} 
	sum # => 15

However this is such a common action that Ruby has a special iterator method called inject, which saves a little code:

	collection = [1, 2, 3, 4, 5]
inject(0) {|sum, i| sum + i} # => 15



Notice that in the inject solution, we didn't need to define the variable total variable outside the scope of iteration. Instead, its scope moved into the iteration. In the example above, the initial value for total is the first argument to inject. We changed the += to + because the block given to inject is evaluated on each value of the collection, and the total variable is set to its output every time.

You can think of the inject example as equivalent to the following code:

	collection = [1, 2, 3, 4, 5]
	sum = 0
	sum = sum + 1
	sum = sum + 2
	sum = sum + 3
	sum = sum + 4
	sum = sum + 5

Although inject is the preferred way of summing over a collection, inject is generally a few times slower than each. The speed difference does not grow exponentially, so you don't need to always be worrying about it as you write code. But after the fact, it's a good idea to look for inject calls in crucial spots that you can change to use faster iteration methods like each.

Nothing stops you from using other kinds of operators in your inject code blocks. For example, you could multiply:

	collection = [1, 2, 3, 4, 5]
	collection.inject(1) {|total, i| total * i} # => 120

Many of the other recipes in this book use inject to build data structures or run calculations on them.

See Also

  • Recipe 2.8, "Finding Mean, Median, and Mode"
  • Recipe 4.12, "Building Up a Hash Using Injection"
  • Recipe 5.12, "Building a Histogram"



Date and Time



Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming


Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration

Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: