Here s an example for the most basic use of a double splat.
Ruby splat operator.
In other words the last example translates to.
You ve probably used splats for catch all arguments.
B is interpreted as a plus b where the plus.
The split mode.
As of ruby 1 9 you can do some pretty odd things with array destructuring and splatting.
Ruby supports a rich set of operators as you d expect from a modern language.
2 one 1 two 2 putting it all together.
Here is how to create a method that adds a variable number of numbers passed as arguments.
The splat operator converts the array into a list of its elements.
So it would be like taking away the array replacing it with everything inside it.
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.
Attr reader title author category that s the power of the splat operator matching operator what is this funny looking ruby.
Most operators are actually method calls.
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.
In one of my recent posts someone pointed out that using the splat operator would have made some of the code a bit simpler.
For example a plus.
It s pretty similar to the original splat with one difference.
It can be used for hashes.
Avoiding creation of array improves performance.
This article has been republished on monkey and crow.
Case when with splat operator use instructions like duparray concatarray which result in creation of arrays.
One of the things that i love about ruby is the depth of its features.
The double splat operator came out back in ruby 2 0.
For each operator 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.
There is a corresponding form of abbreviated assignment operator.
Zoo pet1 pet2 pet3 the splat operator can be used in a case statement.
The humble splat operator and is a great example.
Splat operator in ruby.
The splat operator can be used to create methods that accept a variable number of arguments.