T
Trans
ActiveSupport defines this for Enumerable#sum:
module Enumerable
def sum(identity = 0, &block)
return identity unless size > 0
if block_given?
map(&block).sum
else
inject { |sum, element| sum + element }
end
end
end
The use of #size shouldn't be used in an Enumerable method --since it
is not part of Enumerable's defined interface..... Ah, I was just
about to ask what anyone thought the fix to this is, but it occurs to
me that it might be:
def sum(identity = 0, &block)
if block_given?
map(&block).sum
else
inject { |sum, element| sum + element } || identity
end
end
Look right?
T.
module Enumerable
def sum(identity = 0, &block)
return identity unless size > 0
if block_given?
map(&block).sum
else
inject { |sum, element| sum + element }
end
end
end
The use of #size shouldn't be used in an Enumerable method --since it
is not part of Enumerable's defined interface..... Ah, I was just
about to ask what anyone thought the fix to this is, but it occurs to
me that it might be:
def sum(identity = 0, &block)
if block_given?
map(&block).sum
else
inject { |sum, element| sum + element } || identity
end
end
Look right?
T.