Created
November 12, 2012 14:44
-
-
Save aalvesjr/4059788 to your computer and use it in GitHub Desktop.
Ex.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
numeros = [1,2,3,4,5,6,7,8,9] | |
total = 0 | |
numeros.each do |n| | |
total += n | |
end | |
puts total.to_s | |
# ou | |
numeros = [1,2,3,4,5,6,7,8,9] | |
total = numeros.map.inject(:+) | |
# bagunçando a class Array | |
class Array | |
def soma | |
total = 0 | |
self.each do |n| | |
total += n | |
end | |
total # para ele retornar o total =] | |
end | |
end | |
# dai pode chamar assim: | |
numeros = [1,2,3,4,5,6,7,8,9] | |
total = numeros.soma.to_s | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment