Ruby 将变量初始化为当前时间

Ruby Initialize a Variable to Current Time

我有一个 Rails 3 应用程序需要使用当前时间。每次管理员使用函数 populateTable 时,lastPopulation 变量都需要更新为当前时间。两个问题:

1) 如何立即将 lastPopulation 变量初始设置为当前时间?

2)每当调用populateTable方法时,是否有类似@lastUpdate = Time.now的赋值?

感谢所有帮助。谢谢!!!

你好像在找DateTime#current

lastPopulation = DateTime.current

可以写after_filter来设置lastPopulation, Like

after_filter :set_last_polulated, :only => ['populateTable']

def set_last_polulated
@lastPolulation = DateTime.now
end