在Ruby中,如何添加active_support/core_ext/time?

hgtggwj0  于 2023-04-20  发布在  Ruby
关注(0)|答案(3)|浏览(127)

我找到了一些Ruby代码,它们使用了:

require "active_support/core_ext/time"
require "active_support/core_ext/numeric"

在我的macOS Monterey Mac上,我还没有rails或rvm,如果我安装rvm,它需要Homebrew安装一个新的Ruby,这可能会使事情进一步复杂化,所以对于系统Ruby(ruby 2.6.8p205),如何安装active_support/core_ext/time以便使用它?
我看了this question,它没有解决这个问题。
我可以安装rails:

gem install rails

但是,我真的必须使用sudo来完成吗?(我尽量减少使用sudo来保持系统更安全)。

apeeds0o

apeeds0o1#

使用sudo安装gem是一个非常糟糕的主意
最好在你的系统中使用一些Ruby管理器
例如:

您还可以使用docker来隔离项目
当你得到一些使用这个变体的Ruby时,你可以用Gemfile安装ActiveSupportgem,或者只使用terminal命令

gem install activesupport

然后,您可以在此处选择所需的文件:
https://github.com/rails/rails/tree/main/activesupport/lib
例如,您需要所有Time monkeypatches
它在这里:
https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/time.rb
因此,您需要用途:

require "active_support/core_ext/time"
14ifxucb

14ifxucb2#

为了补充@mechnicov的答案,是的,首先安装像RVM这样的东西,然后

gem install activesupport

你可以用

require "active_support/core_ext/time"

(假定加载https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/time.rb中的所有内容)
如果您使用的是最新版本的Ruby,则根据您需要的数值,而不是:

require "active_support/core_ext/numeric"

它应该装载https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/numeric.rb
您可以用途:

require "active_support/core_ext/numeric/time.rb"

Rails docs中。
您还可以通过以下方式包含所有内容:

require "active_support/all"

this Rails docs。有关详细信息,请参阅https://github.com/rails/rails/blob/main/activesupport/lib/active_support/all.rb

yzckvree

yzckvree3#

你可以做同样的事情使用
sudo gem install active_support
你必须使用sudo,因为你正在写一个默认的系统库。

我强烈推荐使用rbenv或rvm,这样你就不会经常干预系统Ruby。它稍微需要更多的设置,但Homebrew和RVM都是众所周知的支持库。

相关问题