Given the Ruby code
line = "first_name=mickey;last_name=mouse;country=usa"
record = Hash[*line.split(/=|;/)]
I understand everything in the second line apart from the *
operator - what is it doing and where is the documentation for this? (as you might guess, searching for this case is proving hard...)
The splat operator unpacks an array passed to a function so that each element is sent to the function as an individual parameter.
A simple example:
That's it!
The
*
is the splat operator.It expands an
Array
into a list of arguments, in this case a list of arguments to theHash.[]
method. (To be more precise, it expands any object that responds toto_ary
/to_a
, orto_a
in Ruby 1.9.)To illustrate, the following two statements are equal:
It can also be used in a different context, to catch all remaining method arguments in a method definition. In that case, it does not expand, but combine:
Some more detailed information here.
As everyone mentions, it's a "splat". Looking for Ruby syntax is impossible, and I've asked this in other questions. The answer to that part of the question is that you search on
in Google. Google is there for you, just put what you see into words.
Anyhoo, like a lot of Ruby code, that code is quite dense. The
makes an array of SIX elements,
first_name, mickey, last_name, mouse, country, usa
. Then the splat is used to make that into a Hash. Now the Ruby people always send you to look at the Splat method, since everything is exposed in Ruby. I have no idea where it is, but once you have that, you'll see that it runs afor
through the array and builds the hash.You would look for the code in the core documentation. If you cannot find it (I could not), you would try to write some code like this (which works, but is NOT Ruby-like code):
and then the Ruby gang will be able to tell you why your code is silly, bad, or just plain wrong.
If you've read this far, take a read through the Hash documentation for initialization.
Basically a hash that is initialized with several arguments creates them as key value pairs:
So in your example this would lead to the following Hash: