options = arguments[-1].is_a?(Hash) ? arguments.pop : {}
Create a new hash called options
. This will either be assigned to the last element in the arguments array, or an empty one if that is not a hash. In ruby, like python, using -1 as an array index gets you the last element in an array.
options[:add] = true if options.empty?
Set the value in the hash that matches the key :add
to true if the hash you just created is empty.
return add(*arguments) if options[:add]
return subtract(*arguments) if options[:subtract]
return the result of add
or subtract
with the same parameters you passed to this function, based on the state of the options
hash you just created.
For example:
arguments = [{}, {:add => false, :subtract => true}]
would induce the subtract method if used as your parameter.