Created
March 29, 2013 01:00
-
-
Save agumonkey/5268046 to your computer and use it in GitHub Desktop.
suggestion for Joe Cancilla question https://class.coursera.org/proglang-2012-001/forum/thread?thread_id=3627&post_id=16486#post-16486
This file contains 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 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