Avoiding creation of array improves performance.
Ruby splat operator.
So it would be like taking away the array replacing it with everything inside it.
It s pretty similar to the original splat with one difference.
As of ruby 1 9 you can do some pretty odd things with array destructuring and splatting.
The splat operator converts the array into a list of its elements.
Ruby supports a rich set of operators as you d expect from a modern language.
I ve seen the splat operator being used in method definitions to group remaining arguments but it turns out there are actually quite a few other uses as well.
Here is how to create a method that adds a variable number of numbers passed as arguments.
Splat operator in ruby.
Pet1 pet2 pet3 duck dog cat the collect mode.
The splat operator can be used to create methods that accept a variable number of arguments.
Attr reader title author category that s the power of the splat operator matching operator what is this funny looking ruby.
The double splat operator came out back in ruby 2 0.
This article has been republished on monkey and crow.
There is a corresponding form of abbreviated assignment operator.
2 one 1 two 2 putting it all together.
Here s an example for the most basic use of a double splat.
For each operator plus.
One of the things that i love about ruby is the depth of its features.
In other words the last example translates to.
The split mode.
Def doublesplat nums p numsenddoublesplat one.
You ve probably used splats for catch all arguments.
Method in the object referred to by variable a is called with b as its argument.
For example a plus.
You may use an operator but do a little digging and you ll find that you ve only been scratching the surface of what it s capable of.
B is interpreted as a plus b where the plus.
Putting the star before an object invokes the splat operator which has a variety of effects first we ll start with some very useful examples then we will poke around the dark corners of ruby s arrays and the splat operator.
Case when with splat operator use instructions like duparray concatarray which result in creation of arrays.
Most operators are actually method calls.
Zoo pet1 pet2 pet3 the splat operator can be used in a case statement.