如何使用Active Support核心扩展?
我安装了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中。
由于使用Rails应该自动处理,我将假设您正在尝试将Active Support添加到非Rails脚本。
阅读“ 如何加载核心扩展 ”。
在Rails 3中,Active Support的方法被分成了较小的组,因此我们最终不需要使用简单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>
您可以通过已经提到的粒度添加库
require 'active_support/core_ext/some_class/some_file'
还有另一个级别,你可以
require 'active_support/core_ext/some_class'
但是,目前,遗憾的是, Time
, Date
和DateTime
Time
不可用。
解决这个问题的方法是require 'active_support/time'
,它将为你提供Time
, Date
和DateTime
Time
,这将解决OP要求而不需要一切。
My Rails补丁添加了active_support/core_ext/date
和date_time
,使其成为Rails v4.0.0 ,所以现在你可以单独使用它们了。 好极了!
这可以在控制台上运行吗? 这对我有用:
$ 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
您可以 :
需要’active_support / core_ext’
要么 :
要求’active_support / all’
- 无法使用cache_classes = true为Concern(ActiveSupport :: Concern :: MultipleIncludedBlocks)定义多个“包含”块
- 未初始化的常量TZInfo :: InvalidTimezoneIdentifier(NameError) – Rails 4
- Rails time_ago_in_words产生错误的输出
- ActiveRecordvalidation为零
- 我需要做什么才能让Hash.from_xml()工作?
- Ruby没有找到新版本的OpenSSL
- 如何解决activesupport 3.0.0与2.x相比的行为差异?
- NoMethodError:ActiveSupport的未定义方法`halt_callback_chains_on_return_false =’:模块
- 在Rails 4.1中,validates_inclusion_of不再一样吗?