• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Get the Sum of an Array in Ruby

Get the Sum of an Array in Ruby

October 17, 2022 Leave a Comment

There are many ways to calculate the sum of an array of numbers in Ruby. The easiest way is by using the sum method.

nums_array = [1,2,3,4,5,6,7]

print nums_array.sum

#Output:
28

This is definitely the easiest way if dealing with a simple array of numbers. There are a bunch of other ways to sum an array, so let’s take a look at a couple of other options.


We can use the each method to traverse the array and easily sum up all of its numbers.

nums_array = [1,2,3,4,5,6,7]

sum = 0

nums_array.each do |num|
  sum += num
end

print sum

#Output:
28

We can also use the reduce method to sum an array using a little less code than the each method.

nums_array = [1,2,3,4,5,6,7]

print nums_array.reduce { |sum, num| sum + num }

#Output:
28

Finally, let’s look at using a for loop to sum the numbers of an array.

nums_array = [1,2,3,4,5,6,7]

sum = 0

for num in nums_array
  sum += num
end

print sum

#Output:
28

When working with collections of data in Ruby, the ability to summarize the data easily is valuable. One such case is if you want to get the sum of an array of numbers. To calculate the sum of an array of numbers in Ruby, the easiest way is with the sum method. sum returns the sum of an array of numbers.

Below again, is the easiest way to get the sum of an array of numbers in Ruby using the sum method.

nums_array = [-91,12,37,14,-45,65,7]

print nums_array.sum

#Output:
-1

Hopefully this article has been useful for you to learn how to sum an array in Ruby.

Other Articles You'll Also Like:

  • 1.  Using Ruby to Reverse String
  • 2.  Ruby Round – Round Number to Nearest Integer with the round Method
  • 3.  Ruby Infinite Loop
  • 4.  Ruby has_value Method – Check if Hash Has Value
  • 5.  Ruby has_key Method – Check if Hash Has Key
  • 6.  Ruby unshift Method – Add Items to Start of Array
  • 7.  How to Square a Number in Ruby
  • 8.  Ruby Floor – Find Floor of Number with floor Method
  • 9.  Ruby String Comparison
  • 10.  How to Print Without Newline in Ruby

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy