如何使用 Active Support 核心扩展
我安装了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于使用 Rails 应该自动处理这个问题,因此我假设您正在尝试将 Active Support 添加到非 Rails 脚本中。
请阅读“如何加载核心扩展”。
Active Support 的方法在 Rails 3 中被分成更小的组,因此我们最终不会通过简单的
require 'activesupport'
加载大量不需要的内容。现在我们必须做一些事情,比如如果你不关心粒度,你可以选择加载更大的块。如果你想一口气吞下所有东西,请使用...
对于 1.9.2:
对于 1.8.7:
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 likeIf 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:
For 1.8.7:
您可以通过已经提到的精细添加库
还有另一个级别,您可以
但是,目前,不幸的是,这不适用于
Time
、Date
和日期时间
。解决这个问题的方法是
require 'active_support/time'
,它将为您提供Time
、Date
和DateTime
,其中将解决OP所要求的而不需要一切。我的 Rails 补丁,其中添加了
active_support/core_ext /date
和date_time
已纳入 Rails v4.0.0,因此现在您可以单独需要它们。耶!You can granularly add libraries via the already mentioned
There is also another level up where you can
But, at the moment, this is unfortunately not available for
Time
,Date
andDateTime
.A way around this is to
require 'active_support/time'
which will give youTime
,Date
andDateTime
which would solve the OP was asking for without requiring everything.My Rails patch, which adds
active_support/core_ext/date
anddate_time
, made it into Rails v4.0.0, so now you can require these individually. YAY!就我而言,以下链接有效:
https://bundler.io/blog/2019/01/04/an-update-on-the-bundler-2-release.html
In my case the following link worked:
https://bundler.io/blog/2019/01/04/an-update-on-the-bundler-2-release.html
您可以:
需要“active_support/core_ext”
或:
需要“active_support/all”
You can :
require 'active_support/core_ext'
or :
require 'active_support/all'
这可以在控制台上运行吗?这对我有用:
Does this work from the console? This is working for me: