Jiří Vojta Jiří Vojta - 2 years ago 53
Ruby Question

Ruby what does star prefix means in a loop variable?

I've ecountered this today and I have no idea what it means. I tried to google it but I had no luck. Can someone explain this to me?

combinations.each do |combination|
messages = EventNotification.where('user_id = ? AND message_template = ?', *combination)
...
end

Answer Source

It's called the splat operator, and it unpacks an array into single method arguments. In this case, because the function presumably expects two more arguments after the format string, it's equivalent to:

messages = EventNotification.where('user_id = ? AND message_template = ?',
                                   combination[0], combination[1])

In other languages, this feature is often called "varargs".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download