102

我安装了 Active Support 3.0.3 和带有 Ruby 1.8.7 的 Rails 3.0.3。

当我尝试使用时,1.week.ago我得到

NoMethodError: undefined method 'week' for 1:Fixnum
from (irb):2

其他核心扩展似乎有效。我在朋友的计算机上尝试过(他的安装规格和旧版本相同),结果相同。

是什么赋予了?

所有这些都在 IRB 中。

4

5 回答 5

145

由于使用 Rails 应该会自动处理这个问题,我假设您正在尝试将 Active Support 添加到非 Rails 脚本中。

阅读“如何加载核心扩展”。

Active Support 的方法在 Rails 3 中被分解成更小的组,所以我们最终不会用一个简单的require 'activesupport'. 现在我们必须做类似的事情

require 'active_support/core_ext/object/blank'

如果你不关心粒度,你可以选择加载更大的块。如果你想一口吞下所有东西,请使用...

对于 1.9.2:

rvm 1.9.2
irb -f
irb(main):001:0> require 'active_support/all'
=> true
irb(main):002:0> 1.week.ago
=> 2010-11-14 17:56:16 -0700
irb(main):003:0> 

对于 1.8.7:

rvm 1.8.7
irb -f
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'active_support/all'
=> true
irb(main):003:0> 1.week.ago
=> Sun Nov 14 17:54:19 -0700 2010
irb(main):004:0> 
于 2010-11-21T18:54:17.883 回答
29

您可以通过已经提到的精细添加库

require 'active_support/core_ext/some_class/some_file'

您还可以在另一个级别上

require 'active_support/core_ext/some_class'

但是,目前,不幸的是,这不适用于Time,DateDateTime

解决这个问题的一种方法是require 'active_support/time'which will give you Time, Dateand which will DateTimesolve the OP is ask for without requires everything.


我的 Rails 补丁,它添加了active_support/core_ext/datedate_time,使它成为Rails v4.0.0,所以现在你可以单独要求这些。耶!

于 2012-07-14T12:47:13.000 回答
-1

就我而言,以下链接有效:

https://bundler.io/blog/2019/01/04/an-update-on-the-bundler-2-release.html

$ cat Gemfile.lock | grep -A 1 "BUNDLED WITH"
BUNDLED WITH
   1.17.3

$ gem install bundler -v '1.17.3'
于 2019-01-25T06:51:07.007 回答
-4

这可以从控制台工作吗?这对我有用:

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.6.5
BuildVersion:   10H574

$ rails c
Loading development environment (Rails 3.0.3)
>> 1.week.ago
=> Sun, 14 Nov 2010 16:57:18 UTC +00:00
于 2010-11-21T16:57:58.747 回答
-4

您可以:
要求 'active_support/core_ext'
或:
要求 'active_support/all'

于 2011-10-21T02:55:00.627 回答