TIL: Ruby Splat Operator Builds Arrays
You can use the splat operator to coerce values into arrays
You can use the splat operator to coerce values into arrays.
The *thing
convention tries to wrap thing
in an array intelligently. For example:
x = *'billy ruffian'
# x = ['billy ruffian']
If you splat a nil, you get a zero length array:
x = *nil
# x = []
If you splat an array, it's unchanged:
x = *[1,2,3]
# x = [1,2,3]
If you splat a hash, the hash is unpacked:
x = *{name: 'billy'}
# x = [[:name, 'billy]]
If you splat a subarray during array construction, it gets flattened automatically:
x = [*[1,2], *[3,4]]
# x = [1,2,3,4]
Member discussion