Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Ruby | Enumerable sum() function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The sum() of enumerable is an inbuilt method in Ruby returns the sum of all the elements in the enumerable. If a block is given, the block is applied to the enumerable, then the sum is computed. If the enumerable is empty, it returns init.

Syntax: enu.sum { |obj| block }

Parameters: The function accepts a block.

Return Value: It returns the sum of the enumerable.

Example #1:




# Initialize 
enu = (1..5)
  
# Prints 
enu.sum

Output:

15

Example #2:




# Ruby program for sum method in Enumerable
  
# Initialize 
enu = [10, 13, 12, 11]
  
# Prints 
enu.sum {|obj| obj * 5}

Output:

230
My Personal Notes arrow_drop_up
Last Updated : 05 Dec, 2019
Like Article
Save Article
Similar Reads