How to mix required argument and optional arguments in ruby?

后端 未结 2 1129
难免孤独
难免孤独 2021-01-14 19:43

I am working on ruby, and the following is my code snippet. I tried mixing required arguments and option arguments. But it gives me an error:

def my_method(a         


        
2条回答
  •  温柔的废话
    2021-01-14 19:57

    Array splats (the *p) there are only valid as the last argument in the argument list, since it means "collect all the rest of the arguments passed to the method into an array p".

    Just remove d from the method signature, and in your method, just set d = p.pop and it'll work. Or, move d before *p.

提交回复
热议问题