In Ruby, the splat *
and double splat **
operators are used for handling variable-length arguments in methods, especially when dealing with arrays
and hashes.
The Single Splat operator (*) constructs and spreads out Arrays.
If we use it when defining a method, it constructs into an array.
If we use it when passing arguments as an array, it deconstructs the array into arguments.
The double Splat operator (**
) It works similarly. We can construct
and spread out Hashes.
If we use it when defining a method, it constructs into an hash.
If we use it when passing arguments as an hash, it deconstructs the hash into arguments.
Before
When we use the splat operator *
and double splat operator **
with nil
, they have specific behaviors.
When splat *
operator used with nil
, it essentially treats nil
as an empty array.
When double splat **
operator used with nil
, it raises an error because nil
cannot be implicitly converted into a hash.
After
Ruby 3.4 fixes this inconsistent behavior between array splat *nil
and hash splat **nil
with this PR by not raising any error with **nil
.
When **nil
is used, it is treated similarly to **{}
, which passes no keywords
and does not call any conversion methods.