开发者

Defining a recursive method that takes several arguments

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜