Reputation: 720
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
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
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