Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby * operator before array [duplicate]

Tags:

ruby

hash

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.

like image 320
yabbi Avatar asked Jan 19 '13 06:01

yabbi


People also ask

How do you check if there are duplicates in an array Ruby?

select { array. count } is a nested loop, you're doing an O(n^2) complex algorithm for something which can be done in O(n). You're right, to solve this Skizit's question we can use in O(n); but in order to find out which elements are duplicated an O(n^2) algo is the only way I can think of so far.

How do you remove duplicates from an array in Ruby?

With the uniq method you can remove ALL the duplicate elements from an array. Let's see how it works! Where the number 1 is duplicated. Calling uniq on this array removes the extra ones & returns a NEW array with unique numbers.

What does .select do in Ruby?

Array#select() : select() is a Array class method which returns a new array containing all elements of array for which the given block returns a true value. Return: A new array containing all elements of array for which the given block returns a true value.

How do you find the duplicate number on a given integer array in Ruby?

Ruby Array objects have a great method, select . In this case, you are interested in duplicates (objects which appear more than once in the array). The appropriate test is a. count(obj) > 1 .


2 Answers

* 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.

like image 197
rohit89 Avatar answered Oct 29 '22 10:10

rohit89


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.

like image 34
Rachel Gallen Avatar answered Oct 29 '22 10:10

Rachel Gallen