11

Possible Duplicate:
Understanding ruby splat in ranges and arrays

could anyone tell me what the * does in the following piece of code?

line = "name=yabbi;language=ruby;"
Hash[*line.split(/=|;/)]

Thanks.

Community
  • 1
  • 1
yabbi
  • 141
  • 2
  • 7

2 Answers2

12

* is the splat operator. It is used to split an array into a list of arguments.

line.split(/=|;/) returns an array. To create a Hash, each element of the array must be passed as an individual parameter.

rohit89
  • 5,745
  • 2
  • 25
  • 42
  • thanks! just so that I have it clear, all it really does is take that array and turn it into a 'list' of arguments so that the Hash can accept it (in this case anyways, it seems to have many uses)? – yabbi Jan 19 '13 at 06:54
  • Yup. `Hash[]` does not take arrays as an argument so we have to "explode" the elements into individual parameters. – rohit89 Jan 19 '13 at 06:57
1

it's a splat operator Read about it. Often times you see it used when you want to split up an array to use as parameters of a function.

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81