我看到一個方法定義和使用這樣的:單數組參數與多個參數
def mention(status, *names)
...
end
mention('Your courses rocked!', 'eallam', 'greggpollack', 'jasonvanlue')
爲什麼不直接使用數組作爲第二個參數,而不是參數組合成用圖示的陣列?
def mention(status, names)
...
end
mention('Your courses rocked!', ['eallam', 'greggpollack', 'jasonvanlue'])
這也將允許更多的論據在最後。
def mention(status, names, third_argument, fourth_argument)
...
end
mention('Your courses rocked!', ['eallam', 'greggpollack', 'jasonvanlue'], Time.now, current_user)
自從Ruby 1.9版本以來,splat參數不一定是最後一個。例如,你可以有'def提及(status,* names,third_argument,fourth_argument)'。作爲Ruby的新手,也許是編程,你可能不熟悉術語'code smell',正如@Adam在他的回答中所使用的那樣。這始終是一個發情期。人們從來不會聽到,「男孩,你的代碼味道很棒!」 –