Skip to content

Instantly share code, notes, and snippets.

@agumonkey
Created March 29, 2013 01:00
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 agumonkey/5268046 to your computer and use it in GitHub Desktop.
Save agumonkey/5268046 to your computer and use it in GitHub Desktop.
class Object
def mysum
0
end
end
class Fixnum # The class belonging to small integers
def mysum
self # The sum of an integer is just itself
end
end
class Array # The class of for Arrays
def mysum # Sum up the result of calling mysum on all the elements
inject(0) do |acc,elt|
acc + elt.mysum
end
end
end
a = [1, [2, "2.5", 3], [4, "hello", [5, [6, "world", 7]]], 8, [9, 10], "42"]
puts a.mysum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment