简体   繁体   中英

Ruby syntax error, unexpected '=', expecting ')'

I am attempting to write my own solution to a Ruby exercise from Rubymonk where the purpose is to create three methods (add, subtract, and calculate) so when 'calculate' is called you can determine whether or not numbers are added or subtracted based on what is passed in. I am receiving the following error:

main:11: syntax error, unexpected '=', expecting ')' def calculate(*numbers, options={})

Can anyone tell me what the issue is with my code? Thanks for any and all help!

def add(*numbers)
  numbers.inject(0) {|sum, number| sum + number}
end

def subtract(*numbers)
  numbers.inject{|diff, number| diff - number}
end

def calculate(*numbers, options={})
  result = add(numbers) if options.empty?
  result = add(numbers) if options[:add]
  result = subtract(numbers) if options[:subtract]
  result
end
def calculate(*numbers, options={})

is not a valid method definition b/c *numbers takes the place a variable number of arguments. You have two options as I see it -

def calculate(options={}, *numbers)

or

def calculate(*args)
   numbers, options = args[0..-2], args[-1] || {}

if you want to keep the same argument order

The splat argument *numbers needs to be the last argument. Otherwise, how would Ruby know when to treat the last argument as options or as the last number?

You can use (*numbers, options) (without a default value), but that would require that you always pass an options hash to the method (otherwise your last number will be set as the options variable instead).

Try this way:
def calculate(options={},*numbers)

Using optional arguments after the fully optional argument ( the * notation) do not work since it creates an ambiguity.

Read more at:

http://www.skorks.com/2009/08/method-arguments-in-ruby/

You can't use both a splat and a param with a default as last argument, this is too ambiguous for the parser (how to know that the last arg passed is meant to be the options?)

you can work around this in many ways ; one idiom from rails (active support) is :

def calculate(*args)
  options = args.extract_options!
  # ...
end

where extract_options! is a monkey-patch to Array from ActiveSupport defined as follow :

def extract_options!
  last.is_a?(::Hash) ? pop : {}
end

as a side note :

  • an options hash is not really usefull here. you could pass in just a symbol as first argument, maybe.
  • if you use a hash, logic could be simpler :

      def calculate(*args) options = args.extract_options! method = options.fetch(:method, :add) send method, *args end 
  • on add , you don't need inject(0) , inject uses the first element of your array as a first "memo" value if you don't provide one

  • you can pass a symbol to inject, which will be the method called on your "memo" value, with "next value" as argument :

      (1..10).inject(:+) # this is the same as (1..10).inject{ |memo, next| memo + next } # or, more exactly (1..10).inject{ |memo, next| memo.send :+, next } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM