如何使用 Active Support 核心扩展 [英] How to use Active Support core extensions

查看:21
本文介绍了如何使用 Active Support 核心扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆