Title / Description
Code 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
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code