我看到一个方法定义和使用这样的:单数组参数与多个参数
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在他的回答中所使用的那样。这始终是一个发情期。人们从来不会听到,“男孩,你的代码味道很棒!” –