Techioz Blog

active_support/time_with_zone を要求した後の Time:Class の未定義メソッド

概要

私は 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 クラスのゾーン クラス メソッドは、実際には active_support/core_ext/time/zones にあります。本当に Time.zone を使用したい場合は、そのクラスを必須にすることもできますが、より良いアプローチでは、active_support/all を必須にする必要があります。

提案される解決策

require "active_support/all"

active_support のソース コード フォートをチェックアウトしたい場合は、github リポジトリを参照してください。