2012-12-11 60 views
1

變化(:小時=> 1)在導軌控制檯未定義的方法`變化 '在IRB控制檯的DateTime

irb(main):004:0> dt = DateTime.now 
=> Mon, 10 Dec 2012 15:52:01 -0800 
irb(main):005:0> _.change(:hour => 1) 
=> Mon, 10 Dec 2012 01:00:00 -0800 
irb(main):006:0> 
irb(main):007:0> dt.class 
=> DateTime 
irb(main):008:0> 

工作正常,但將引發「未定義的方法`變化' 在IRB

irb(main):008:0> require 'active_record' 
=> true 
irb(main):009:0> dt = DateTime.now 
=> #<DateTime: 2012-12-10T15:55:14-08:00 (10611097185737/4320000,-1/3,2299161)> 
irb(main):010:0> dt.class 
=> DateTime 
irb(main):011:0> dt.change(:hour => 1) 
NoMethodError: undefined method `change' for #<DateTime:0x53e20a9a> 
     from (irb):11:in `evaluate' 
     from org/jruby/RubyKernel.java:1070:in `eval' 
     from org/jruby/RubyKernel.java:1395:in `loop' 
     from org/jruby/RubyKernel.java:1178:in `catch' 
     from org/jruby/RubyKernel.java:1178:in `catch' 
     from C:\PF\jruby-1.7.0.preview2\bin\irb:13:in `(root)' 
irb(main):012:0> 

」任何建議如何使其工作?

在此先感謝。

回答

0

Change is an active support method,所以要把它定義,你首先必須要求庫:

>> require 'active_support/all' 
=> true 
>> dt = DateTime.now 
=> Tue, 11 Dec 2012 11:34:54 +0100 
>> dt.change(:hour => 1) 
=> Tue, 11 Dec 2012 01:00:00 +0100 
+0

米哈爾,非常感謝。現在正在工作。 –

+0

如果此解決方案適合您,請單擊接受答案按鈕。 – Noz

10

要獲得有關日期時間功能,只需require 'active_support/time'

+0

不錯,謝謝。有用。請給我提示,我怎麼能找到解決這樣的問題呢? –

+0

您可以參考導軌指南[http://guides.rubyonrails.org/active_support_core_extensions.html](http://guides.rubyonrails.org/active_support_core_extensions.html) – siddick

+0

謝謝!尼斯鏈接。 –

相關問題