Where is it legal to use ruby splat operator?

后端 未结 2 1733
生来不讨喜
生来不讨喜 2020-12-03 01:31

Splats are cool. They\'re not just for exploding arrays, although that is fun. They can also cast to Array and flatten arrays (See http://github.com/mischa/splat/tree/master

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 02:13

    The "splat operator" is in fact not an operator at all but a token defined in the Ruby grammar. A read through grammar.y or the Ruby grammar in BNF form* will tell you that it is allowed as the last or only argument:

    • in a method definition (except for an optional last &foo)
    • in a method call (except for an optional last &foo)
    • on the LHS of as assignment, for example: a, b, *cs = [1,2,3,4]
    • on the RHS of an assignment, for example: a, b, c = 1, 2, *[3,4,5]
    • in the when clause of a case statement

提交回复
热议问题