2017-08-16 77 views
1

我今天發現了這個,我不知道它是什麼意思。我試圖谷歌它,但我沒有運氣。誰可以給我解釋一下這個?Ruby星號前綴在循環變量中意味着什麼?

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

回答

3

它被稱爲圖示操作者,然後解壓縮的陣列成單個方法的參數。在這種情況下,由於格式字符串後兩個參數的函數大概預計,這相當於:

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

在其他語言中,這個功能通常被稱爲「可變參數」。