Skip to content

Instantly share code, notes, and snippets.

@aalvesjr
Created November 12, 2012 14:44
Show Gist options
  • Save aalvesjr/4059788 to your computer and use it in GitHub Desktop.
Save aalvesjr/4059788 to your computer and use it in GitHub Desktop.
Ex.
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