As you probably know, you can use Ruby’s splat operator (
*) for any-length argument lists:
It has another handy use, though, as a list flattener.
You can use it for nicer array concatenation:
foos << "more", but that mutates the original array, so it’s often not an option.
You can also use it for method argument concatenation. For example with
attr_accessible in Ruby on Rails:
The above is true of Ruby 1.9 and later. Ruby 1.8.7 only lets the single last list element be splatted.
Basically, splat lets you put an array or argument list anywhere inside another array or argument list in a “flat” way – making the elements part of that list itself. I’ve found it can make for quite pleasant code.