Ruby-on-rails 如何使用 Active Support 核心扩展

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4238867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:41:03  来源:igfitidea点击:

How to use Active Support core extensions

ruby-on-railsrubytimeextension-methodsactivesupport

提问by griotspeak

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

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

When I try to use 1.week.agoI get

当我尝试使用时,1.week.ago我得到

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.

所有这些都在 IRB 中。

回答by the Tin Man

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

由于使用 Rails 应该自动处理这个问题,我将假设您正在尝试将 Active Support 添加到非 Rails 脚本。

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

在 Rails 3 中,Active Support 的方法被分成了更小的组,所以我们最终不会用一个简单的require 'activesupport'. 现在我们必须做这样的事情

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:

对于 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:

对于 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> 

回答by mraaroncruz

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, Dateand DateTime.

但是,目前,不幸的是,这不适用于Time,DateDateTime

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

解决这个问题的一种方法是,require 'active_support/time'它会给你TimeDate并且DateTime可以解决 OP 要求的问题,而不需要一切。



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

我的 Rails 补丁,它添加了active_support/core_ext/datedate_time,使其成为Rails v4.0.0,所以现在您可以单独要求这些。好极了!

回答by Kazuya Gosho

In my case the following link worked:

在我的情况下,以下链接有效:

https://bundler.io/blog/2019/01/04/an-update-on-the-bundler-2-release.html

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'

回答by Paul Schreiber

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

回答by Lane

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

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