开发者

Defining a recursive method that takes several arguments

开发者 https://www.devze.com 2023-02-08 11:37 出处:网络
What do I define a recursive method with variable arguments in ruby? I was thinking def meth(var, *var)

What do I define a recursive method with variable arguments in ruby?

I was thinking

    def meth(var, *var)     
      meth(var,var)
    end

If I do it like that, var becomes an array 开发者_运维技巧in the next iteration.


Use the splat when you call it as well (like you would with & when passing a block rather than defining one):

def meth(var, *var)     
  meth(var,*var)
end

Least surprise!

0

精彩评论

暂无评论...
验证码 换一张
取 消