如何使用Active Support核心扩展 [英] How to use Active Support core extensions
问题描述
我已经安装了Active Support 3.0.3,并且已将Rails 3.0.3与Ruby 1.8.7一起安装.
I have Active Support 3.0.3 installed and Rails 3.0.3 with Ruby 1.8.7.
当我尝试使用1.week.ago
时,我会得到
When I try to use 1.week.ago
I get
NoMethodError: undefined method 'week' for 1:Fixnum
from (irb):2
其他核心扩展似乎也可以使用.我在朋友的计算机上尝试过(相同的安装规格和旧版本),结果相同.
The other core extensions seem to work. I tried it on a friend's computer (same install specs and legacy versions are on his) with the same results.
有什么作用?
所有这些都在IRB中.
All of this is in IRB.
推荐答案
由于使用Rails应该可以自动处理此问题,因此我假设您正在尝试将Active Support添加到非Rails脚本中.
Since using Rails should handle this automatically I'm going to assume you're trying to add Active Support to a non-Rails script.
阅读"如何加载核心扩展".
Active Support的方法在Rails 3中分成了较小的组,因此我们最终并没有通过简单的require 'activesupport'
加载很多不需要的东西.现在我们要做
Active Support's methods got broken into smaller groups in Rails 3, so we don't end up loading a lot of unneeded stuff with a simple require 'activesupport'
. Now we have to do things like
require 'active_support/core_ext/object/blank'
如果您不关心粒度,则可以选择加载更大的块.如果您想一口气使用所有东西...
If you don't care about granularity, you can choose to load bigger chunks. If you want everything in one big gulp use...
对于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>
这篇关于如何使用Active Support核心扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!