Ruby refactoring of a method
-
27-10-2019 - |
Pergunta
Would love to refactor this into just one line:
def sum_something
sum = 0
self.each { |a| sum += a }
return sum
end
There must be a way to define 'sum' within the block and I could drop the 'return'.
Solução
def sum_something
inject(0, :+)
end
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow