-
-
Save hsanchez/1719959 to your computer and use it in GitHub Desktop.
monkey patch for Array if using classifier gem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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