Panos Matsinopoulos

Senior Software Engineer: Love Writing and Reading Software, Taking Entrepreneurial Risks, Teaching Computer Programming & Blogging here or in many other places. I also Love Music and I can call myself a small pianist!

Ruby Array Sum

| Comments

This short post is going to demonstrate how we can sum an array of integers or an array of objects that have an integer attribute.

Let’s start:

Sum array of integers

First version using reduce with block

1
2
3
4
5
array_of_i = [10, 2, 8, 5, 7, 9]

sum = array_of_i.reduce(0) { |result, item| result + item }

puts "Sum of #{array_of_i.join(",")} is: #{sum}"

Second version using reduce giving the method symbol to apply

1
2
3
4
5
array_of_i = [10, 2, 8, 5, 7, 9]

sum = array_of_i.reduce(:+)

puts "Sum of #{array_of_i.join(",")} is: #{sum}"

Sum of objects that have an integer attribute

Using reduce with a block

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Product
  attr_accessor :price

  def initialize(price)
    self.price = price
  end
end

prod1 = Product.new(1000)
prod2 = Product.new(500)
prod3 = Product.new(1500)

array_of_products = [prod1, prod2, prod3]

sum = array_of_products.reduce(0) {|result, item| result + item.price}

puts "Sum of array_of_products is: #{sum}"

I am pretty sure that you can come up with other methods of doing the above. Can you post in the comments any alternatives? Advantages and disadvantages?

Comments