Skip to content

Instantly share code, notes, and snippets.

@barce
Created May 30, 2011 00:35
Show Gist options
  • Save barce/998287 to your computer and use it in GitHub Desktop.
Save barce/998287 to your computer and use it in GitHub Desktop.
monkey patch for Array if using classifier gem
class Array
def sum(identity = 0, &block)
return identity unless size > 0
if block_given?
map(&block).sum
else
a = inject( nil ) { |sum,x| sum ? sum+x : x }
# Hack to remove the to_f error
a.is_a?(Array) ? a : a.to_f
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment