ruby - 方法可变参数(如*options)之后的参数,不可以设置默认值吗?
伊谢尔伦
伊谢尔伦 2017-04-24 09:12:26
0
3
712

譬如:

def calculate(*numbers, method = {:add=>true})

就一直报错:

syntax error, unexpected '=', expecting ')' (SyntaxError)

检索The Ruby Programming Wikibook 的相关语法也没有提示,请问这是什么原因呢?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all (3)
黄舟

Suppose you are the interpreter, what would you do withcalculate({add: true})中的参数{add: true}分配给*numbers呢,还是method?

You can’t judge at all, right? So it's not allowed.

    Peter_Zhu

    Can’t this kind of*numbersvariable parameters only be at the end?

    def calc(method = {add: true}, *numbers); endis allowed, but the reverse is not allowed.

      伊谢尔伦

      Not allowed, there will be ambiguity

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!