I have the following function –
def add (*nums)
nums.reduce(:+)
end
def subtract(first, *rest)
first - rest.reduce(:+)
end
def calculate(*nums, options)
first = nums.first
rest = nums.reverse.drop(1)
add(*nums) if options.size == 0
end
The following is the error I receive when invoking the function –
syntax error, unexpected ‘=’, expecting ‘)’ def calculate(*nums,
options={}) ^
What is the error in my syntax ?
You can have your splat and your options too, you just have to sort it out by hand:
Then you can say
calculate(1, 2, 3)andcalculate(1, 2, :size => 0)without difficulty. Of course, this assumes that a Hash isn’t a valid value for something innums.