Ronan Lopes Ronan Lopes - 1 year ago 94
Ruby Question

Ruby - Optional argument wrong match

I have a ruby function with the following signature:

def botao_adicionar(model, prefix = "/#{}", callback = nil)

and I want to make it a call without pass the
argument, so it takes the default from function that I defined above. So I call it this way:

botao_adicionar(model=ArrendamentoContrato, callback="bind_unidades_consumidoras()")

And it associates the
value to
variable. I believe it's matching by the order of arguments, but I explicitly called the name of the argument in the function call... that shouldn't work? Thanks!

Answer Source

All your arguments are positional. Meaning that you can't assign them by name.

It seems that you want to use keyword arguments. The correct syntax for them is this:

def botao_adicionar(model, prefix: "/#{}", callback: nil)

And then

botao_adicionar(ArrendamentoContrato, callback: "bind_unidades_consumidoras()")