Saurabh7
Saurabh7

Reputation: 720

what does :* stand for in ruby

I recently came across this code

left_params  = [:*]*2
right_params = [:*]*2  
t[*left_params] = self[*right_params]

where t expects 2 integer arguments. I couldn't figure out what this was supposed to do.

Upvotes: 0

Views: 180

Answers (3)

Rustam Gasanov
Rustam Gasanov

Reputation: 15781

[:*]*2

create an array with double star simbols([:*, :*])

t[*left_params] = self[*right_params]

* here mean unpack array, so this is interpreted t[:*, :*] = self[:*, :*]. Without * sign expression would be interpreted like t[[:*, :*]] = self[[:*, :*]]

Upvotes: 1

Niels B.
Niels B.

Reputation: 6310

It's a symbol wrapped in an array.

The code works because arrays support multiplication in Ruby. E.g.

list = ["hello", "world"]
multiplied_list = list * 2
=> ["hello", "world", "hello", "world"]

Upvotes: 2

user229044
user229044

Reputation: 239230

It's a symbol. Like "*".to_sym.

Upvotes: 3

Related Questions