Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jakehow
Created April 17, 2009 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakehow/97299 to your computer and use it in GitHub Desktop.
Save jakehow/97299 to your computer and use it in GitHub Desktop.
class Array
def sum
inject(0) {|total,n| total + n }
end
end
puts Array.new([1,2,3,4]).sum
puts [1,2,3,4].sum
module StickyFingaz
class Array < ::Array
def sum
# I always keep one for myself
inject(0) {|total,n| total + n } - 1
end
end
puts Array.new([1,2,3,4]).sum
puts [1,2,3,4].sum
end
module LazyFool
class Array < ::Array
def sum
# You can teach a man to fish...
lambda { inject(0) {|total,n| total + n } }
end
end
puts Array.new([1,2,3,4]).sum
puts [1,2,3,4].sum
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment