This article has been republished on Monkey and Crow.
As of ruby 1.9, you can do some pretty odd things with array destructuring and splatting. 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.
You can use a splat in a method definition to gather up any remaining arguments:
In the example above,
what will get the first argument, then
*people will capture however many other arguments you pass into
say. A real world example of this can be found in the definition of
Delegator#method_missing. A common ruby idiom is to pass a hash in as the last argument to a method. Rails defines an array helper
Array#extract_options! to make this idiom easier to…
View original post 328 more words