Что делает (унарный) оператор * в этом коде Ruby?

Оператор splat распаковывает ruby массив, переданный функции, так splat что каждый элемент отправляется operators в функцию как отдельный параметр.

Простой operators пример:

>> def func(a, b, c)
>>   puts a, b, c
>> end
=> nil

>> func(1, 2, 3)  #we can call func with three parameters
1
2
3
=> nil

>> list = [1, 2, 3]
=> [1, 2, 3]

>> func(list) #We CAN'T call func with an array, even though it has three objects
ArgumentError: wrong number of arguments (1 for 3)
    from (irb):12:in 'func'
    from (irb):12

>> func(*list) #But we CAN call func with an unpacked array.
1
2
3
=> nil

Вот и все!

ruby

operators

splat

2022-08-22T02:31:43+00:00