A list of stars in the block list

In Ruby, I have code similar to the following

foo { |x, y| puts y }

Since the compiler / interpreter warns me about unused var X, I replaced x with "*" and the compiler stopped complaining. (I don’t know why I decided it was the best choice ... It just happened ...)

foo { |*, y| puts y }

What does it do? Are there any side effects?

+3
source share
1 answer

An asterisk in this context is called the "splat" operator. This means that you can pass several parameters in their place, and the block will see them as an array.

, (, foo { |*x, y| puts y }). , , , , .

+4

Source: https://habr.com/ru/post/1764899/


All Articles