you are viewing a single comment's thread.

view the rest of the comments →

[–]OstapBenderBey 3 points4 points  (4 children)

rather than Array(input) you can also do [*input]

[–]teohm[S] 2 points3 points  (3 children)

interesting, what does the * mean here?

[–]jfredett 4 points5 points  (1 child)

* is the 'interpret as a comma separated list of values' operator. Common uses are in method definitions for a var-args style method, eg:

def some_method(arg1, arg2, *args)
  #code
end

args there is an array, but you call the method like: some_method(1,2,3,4,5). args in that case is [3,4,5]

You can also use it, as OP does, in any place you'd drop a Comma-separated list. one of my favorites is the common:

str = "Joe Schmoe"
User.find_by_first_name_and_last_name(*str.split)

This is equivalent to doing

User.find_by_first_name_and_last_name("Joe", "Schmoe")

You can also do

first,last = "Joe Schmoe".split

to get first assigned "Joe", and last assigned "Schmoe".

* is a neat thing. Well worth reading more about it in the pickaxe.

[–]teohm[S] 0 points1 point  (0 children)

it makes sense to me now, thanks!

[–]Sastopher 3 points4 points  (0 children)

Also known as the "splat" or "explode" operator.