0
根据this page的建议,我需要在Ruby 1.9.2中为DateTime添加getlocal方法。我该怎么做呢?感谢您的阅读如何修改一个Ruby类?
根据this page的建议,我需要在Ruby 1.9.2中为DateTime添加getlocal方法。我该怎么做呢?感谢您的阅读如何修改一个Ruby类?
您可以通过像打开任何其他类定义一样打开类定义来添加数据。
class DateTime
def getlocal
self.to_time.getlocal.to_datetime
end
end
干脆把,在你的文件将猴子修补方法为DateTime
类。
当您打开一个已存在的类时,可以向其中添加代码或覆盖现有的方法。