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'.

Foi útil?

Solução

def sum_something
  inject(0, :+)
end
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top