rubymonk

Ruby code posted by aaaa
created at 03 Jan 21:05

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def add(*numbers)
  numbers.reduce(:+)
  # or numbers.inject(0) { |sum, number| sum + number }
end

def subtract(*numbers)
  numbers.reduce(:-)
  # or numbers.inject { |subtraction, number| subtraction - number }
end

def calculate(*arguments)
  options = arguments.last.is_a?(Hash) ? arguments.pop : {}
  if options[:add] then add(*arguments)
  elsif options[:subtract] then subtract(*arguments)
  else add(*arguments)
  end
end
445 Bytes in 4 ms with coderay