Skip to content

Instantly share code, notes, and snippets.

@AhmedNadar
Created October 31, 2013 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AhmedNadar/7254497 to your computer and use it in GitHub Desktop.
Save AhmedNadar/7254497 to your computer and use it in GitHub Desktop.
Calculate the sum of all elements in an array
# Calculate the sum of all elements in an array
def sum(array)
array.inject{|sum, e| sum + e}
end
# or i can use shortcut
def sum(array)
array.inject(:+)
end
puts array = [12,45,56,34,7,3,89,4,2,44]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment