ruby – 未定义的方法`zone` for Time:Class,需要active_support / time_with_zone

前端之家收集整理的这篇文章主要介绍了ruby – 未定义的方法`zone` for Time:Class,需要active_support / time_with_zone前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 Ruby 2.2.1上安装了active_support 4.2,所以我想使用它
Time.zone.parse('2007-02-10 15:30:45')

如下所述:http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html

但即使在需要active_support和active_support / time_with_zone之后,我似乎仍然无法使用Time.zone.注意:

2.2.1 :002 >   require "active_support"
 => true
2.2.1 :003 > Time.zone
NoMethodError: undefined method `zone' for Time:Class
2.2.1 :004 > require "active_support/time_with_zone"
 => true
2.2.1 :005 > Time.zone
NoMethodError: undefined method `zone' for Time:Class

这是怎么回事?

解决方法

Time类的zone类方法实际上位于active_support / core_ext / time / zones中.如果您真的想使用Time.zone,则可以要求该类,但更好的方法可能需要active_support / all

建议的解决方

require "active_support/all"

如果你想查看active_support的源代码堡,请查看github repo

原文链接:https://www.f2er.com/ruby/268677.html

猜你在找的Ruby相关文章