From: Jeremy Bopp Date: 2012-02-06T13:20:54+09:00 Subject: Re: Error with splat params in method definition On 02/05/2012 07:19 PM, Jones Lee wrote: > I'd like to ask why having: > > ``` > def my_method(*param1, param2 = "default"); end > ``` > > returns > > ``` > SyntaxError: (irb):15: syntax error, unexpected '=', expecting ')' > ``` > > My workaround is explicitly wrap param in brackets like this: > > ``` > def my_method((*param1), param2 = "default"); end > ``` Have you tried calling your method with the workaround with exactly 0 or more than 2 arguments? I think you'll find that your workaround doesn't really work around anything aside from the syntax error. Now, I could be wrong about the details, and I believe there are some subtle changes coming soon with respect to parameter handling for methods, but I'll see if I can clarify what is going on. Ruby attempts to associate method parameters as follows: 1) Assign the first parameters given in the method call to all the positional parameters that do not have defaults defined and report an error if there are not enough given parameters to do this. 2) If there are still more given parameters, assign those to the positional parameters that would otherwise have defaults set. 3) Assign any remaining parameters that are NOT key-value definitions for a hash to the splat parameter. 4) Assign the key-value hash definitions to the final parameter as a single hash. These rules require that method definitions first specify positional parameters without defaults, followed by positional parameters with defaults, followed by the splat parameter, followed by a single parameter that will receive the hash parameters. I'm sure that I'm going to get all sorts of corrections about the above, but that's the gist as I understand things at least. It appears that you would like to instead specify your positional parameters (param2) after your splat parameter (param1). I don't believe Ruby yet has any facility to handle that in the language, but you can emulate it: def my_method(*params) param1 = params param2 = params.size > 1 ? params.pop : "default" ... end -Jeremy