tags:

views:

61

answers:

1

In Ruby, I have code similar to the following

foo { |x, y| puts y }

Because the compiler/interpreter keeps warning me about the unused var X, I replaced x with a '*' and the compiler stopped complaining. (I don't know why I decided * was the best choice... It just happened...)

foo { |*, y| puts y }

What does this do exactly? And are there any side effects?

+2  A: 

The asterisk in this context is called the "splat" operator. This means you can pass multiple parameters in its place and the block will see them as an array.

I'm not sure how or why it works without a variable name after it, though (e.g. foo { |*x, y| puts y }). I would guess that it means the block ignores all parameters except for the last one, which it prints out.

Anna Lear