如何使用 Active Support 核心扩展

发布于 2024-10-03 19:04:24 字数 286 浏览 0 评论 0原文

我安装了 Active Support 3.0.3 和 Rails 3.0.3 以及 Ruby 1.8.7。

当我尝试使用 1.week.ago 时,我发现

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

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

什么给?

所有这些都在 IRB 中。

I have Active Support 3.0.3 installed and Rails 3.0.3 with Ruby 1.8.7.

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.

What gives?

All of this is in IRB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凑诗 2024-10-10 19:04:24

由于使用 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> 

Since using Rails should handle this automatically I'm going to assume you're trying to add Active Support to a non-Rails script.

Read "How to Load Core Extensions".

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...

For 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> 

For 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> 
热鲨 2024-10-10 19:04:24

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

require 'active_support/core_ext/some_class/some_file'

还有另一个级别,您可以

require 'active_support/core_ext/some_class'

但是,目前,不幸的是,这不适用于 TimeDate日期时间

解决这个问题的方法是require 'active_support/time',它将为您提供TimeDateDateTime,其中将解决OP所要求的而不需要一切。


我的 Rails 补丁,其中添加了 active_support/core_ext /datedate_time 已纳入 Rails v4.0.0,因此现在您可以单独需要它们。耶!

You can granularly add libraries via the already mentioned

require 'active_support/core_ext/some_class/some_file'

There is also another level up where you can

require 'active_support/core_ext/some_class'

But, at the moment, this is unfortunately not available for Time, Date and DateTime.

A way around this is to require 'active_support/time' which will give you Time, Date and DateTime which would solve the OP was asking for without requiring everything.


My Rails patch, which adds active_support/core_ext/date and date_time, made it into Rails v4.0.0, so now you can require these individually. YAY!

维持三分热 2024-10-10 19:04:24

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

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'

In my case the following link worked:

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'
寄离 2024-10-10 19:04:24

您可以:
需要“active_support/core_ext”
或:
需要“active_support/all”

You can :
require 'active_support/core_ext'
or :
require 'active_support/all'

长安忆 2024-10-10 19:04:24

这可以在控制台上运行吗?这对我有用:

$ 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

Does this work from the console? This is working for me:

$ 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文