What does this mean in Ruby language?

后端 未结 3 1951
孤城傲影
孤城傲影 2021-01-06 01:05

Run the following code,

a = [1, 2, 3, 4, 5]
head, *tail = a
p head
p tail

You will get the result

1
[2, 3, 4, 5]

3条回答
  •  长发绾君心
    2021-01-06 01:40

    I don't know Ruby at all, but my guess is that the statement is splitting the list a into a head (first element) and the rest (another list), assigning the new values to the variables head and tail.

    This mechanism is usually referred (at least in Erlang) as pattern matching.

提交回复
热议问题