Created
December 10, 2010 00:54
-
-
Save baburdick/735599 to your computer and use it in GitHub Desktop.
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
# Write a MathService module that has a method that can sum an Array of Numbers | |
# It should raise an exception if not given an Array. | |
# It should raise an exception if any elements are not Numeric. | |
module MathService | |
def sum array_of_numbers | |
raise unless array_of_numbers.is_a? Array | |
sum = array_of_numbers.inject(0) do |s,n| | |
raise unless n.is_a? Numeric | |
s += n | |
s | |
end | |
return sum | |
end | |
extend self | |
end | |
###################################################################### | |
begin | |
puts MathService.sum([1, 2, 3]) # => 6 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment