One of the coolest things in Ruby is the splat operator (*). In short, it lets you destructure arrays into variables, for example:
a, * = *[123]
puts a.inspect # 123
You can simplify this code even further by just dropping the asterisks:
a, = [123]
puts a.inspect # 123
If you only need the tail of an array, the leading asterisk is mandatory:
*, b = [123, 321]
puts b.inspect # 321
But you can replace it with an underscore:
_, b = [123, 321]
puts b.inspect # 321
One of its core uses is grabbing both the head and tail of an array in a single expression (provided it has two or more elements, of course):
a, *, c = [1, 2]
puts a.inspect # 1
puts c.inspect # 2
a, *, c = [1, 2, 3, 4]
puts a.inspect # 1
puts c.inspect # 4
And my absolute favorite use case is how easily you can merge multiple arrays into one:
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
c = [*a, *b]
puts c.inspect # [1, 2, 3, 4, 5, 6, 7, 8]
Do you use the splat operator in your code? How do you apply it in your projects?