Name this python/ruby language construct (using array values to satisfy function parameters)

前端 未结 7 1662
予麋鹿
予麋鹿 2020-12-14 02:37

What is this language construct called?

In Python I can say:

def a(b,c): return b+c
a(*[4,5])

and get 9. Likewise in Ruby:

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 03:03

    In ruby, it is often called "splat".

    Also in ruby, you can use it to mean 'all of the other elements in the list'.

    a, *rest = [1,2,3,4,5,6]
    a     # => 1
    rest  # => [2, 3, 4, 5, 6]
    

    It can also appear on either side of the assignment operator:

    a  = d, *e
    

    In this usage, it is a bit like scheme's cdr, although it needn't be all but the head of the list.

提交回复
热议问题