我发现一些 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
怎么安装才能使用?
我看了这个问题并没有解决问题
我记得我可以安装导轨:
gem install rails
不过,难道非得用
sudo
才能做到吗? (我尽量减少sudo
的使用,以保证系统更安全)。
你可以用做同样的事情
sudo gem install active_support
你必须使用 sudo 因为你正在写入一个默认的系统库。
我强烈推荐使用 rbenv 或 rvm,这样你就不会经常干扰系统 Ruby。它的设置稍微多一些,但 Homebrew 和 RVM 都是众所周知且受支持的库。
使用
sudo
安装 gem 完全是个坏主意
最好在你的系统中使用一些 ruby 管理器
例如:
也可以使用docker隔离项目
当您使用其中一种变体获得一些 Ruby 时,您可以使用
Gemfile
或仅使用终端命令安装 ActiveSupport gem
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"
添加到@mechnicov 的回答,是的,首先安装 RVM 之类的东西,然后
gem install activesupport
你可以使用
require "active_support/core_ext/time"
和以前一样。
如果你使用的是最新版本的 Ruby,这取决于你需要的数字,而不是:
require "active_support/core_ext/numeric"
你可以使用:
require "active_support/core_ext/numeric/time.rb"
两个支持是:
activesupport/lib/active_support/core_ext/numeric/bytes.rb
activesupport/lib/active_support/core_ext/numeric/time.rb
在 Rails 文档中.