我在这样的rails helper文件中有一个方法

def table_for(collection, *args)
 options = args.extract_options!
 ...
end

我希望能够像这样调用这个方法

args = [:name, :description, :start_date, :end_date]
table_for(@things, args)

这样我就可以根据表单提交动态传入参数。我无法重写该方法,因为我在太多地方使用它,我还能怎么做呢?

其他提示

只需这样称呼:

table_for(@things, *args)

splat * )运算符可以完成这项工作,而无需修改方法。

class Hello
  $i=0
  def read(*test)
    $tmp=test.length
    $tmp=$tmp-1
    while($i<=$tmp)
      puts "welcome #{test[$i]}"
      $i=$i+1
    end
  end
end

p Hello.new.read('johny','vasu','shukkoor')
# => welcome johny
# => welcome vasu
# => welcome shukkoor
scroll top