How to do array addition in a smart way? [duplicate]
Pergunta
Possible Duplicate:
How to sum array members in Ruby?
Lets say I have this array
@test = [1, 2, 3, 4]
Then I want to do:
@test[0] + @test[1] + @test[2] + @test[3]
Is there not a smarter, faster way of doing this?
Solução
You can do this:
@test.inject(:+)
Outras dicas
sum = 0
@test.each { |el| sum+=el }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow