I am trying to design an api that works like this:
client.entries(content_type: 'shirts', { some: 'query', other: 'more', limit: 5 })
So I have this method in my client
class:
def entries(content_type:, query={})
puts query
end
But I get syntax error, unexpected tIDENTIFIER
I also tried splatting:
def entries(content_type:, **query)
puts query
end
But I get
syntax error, unexpected ')', expecting =>...ry', other: 'more', limit: 5 })
What's the right way to do this without switching around the order of the arguments. The second argument has to be a hash and I don't want to use a keyword argument
as a second parameter
The second works in current MRI and JRuby:
The first one can't work because you can't both have keyword arguments and also automatically collect key-value pairs into a hash argument.
EDIT in response to comment:
If you wanted to pass a hash and not collect extra keywords into a hash, then you need to reverse the signature:
Or, you can splat your hash:
Or, you can make the second argument also into a keyword: