Undefined `zone` method for Time: class after request active_support / time_with_zone

I am on Ruby 2.2.1 and I have active_support 4.2 installed, so I want to use

Time.zone.parse('2007-02-10 15:30:45') 

as described here: http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html

But even after requesting active_support and active_support/time_with_zone , it seems to me that Time.zone is still not working. Note:

 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 

What's happening?

+5
source share
3 answers

The zone class method for the Time class is actually located in active_support/core_ext/time/zones . You can require this class if you really want to use Time.zone , but a better approach might require active_support/all

Recommended Solution

 require "active_support/all" 

If you want to check source code formatting for active_support, have a look at github repo

+2
source

active_support/time_with_zone provides the ActiveSupport::TimeWithZone , but does not extend the Time kernel class.

Instead, you can require active_support/time :

 require 'active_support' require 'active_support/time' Time.zone = 'Eastern Time (US & Canada)' #=> "Eastern Time (US & Canada)" Time.zone.parse('2007-02-10 15:30:45') #=> Sat, 10 Feb 2007 15:30:45 EST -05:00 
+3
source

Try

 require 'active_support/all' 

instead

 require "active_support" 
+1
source

Source: https://habr.com/ru/post/1241700/


All Articles