Skip to content

Instantly share code, notes, and snippets.

@baburdick
Created December 10, 2010 00:54
Show Gist options
  • Save baburdick/735599 to your computer and use it in GitHub Desktop.
Save baburdick/735599 to your computer and use it in GitHub Desktop.
# 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