0

I have a ruby function with the following signature:

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

and I want to make it a call without pass the prefix 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 callback value to prefix 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!

Ronan Lopes
  • 3,050
  • 3
  • 22
  • 47

2 Answers2

1

You can use keyword arguments:

def botao_adicionar(model, prefix: "/#{model.name.tableize}", callback: nil)
end

Usage:

botao_adicionar(ArrendamentoContrato, callback: "bind_unidades_consumidoras()")
Andrey Deineko
  • 49,444
  • 10
  • 105
  • 134
1

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: "/#{model.name.tableize}", callback: nil)

And then

botao_adicionar(ArrendamentoContrato, callback: "bind_unidades_consumidoras()")
Sergio Tulentsev
  • 219,187
  • 42
  • 361
  • 354