在 Ruby 3 中使用 bundle exec 运行脚本时出现 LoadError(使用 rvm)

LoadError when using bundle exec to run a script in Ruby 3 (using rvm)

提问人:Dominus.Vobiscum 提问时间:9/21/2023 更新时间:9/21/2023 访问量:17

问:

下面的 Ruby 脚本给了我一个 何时需要 gem,但其他一切都向我显示它已安装。无论我运行它还是 .LoadErroractivesupportbundle execrvm do

$ cat Gemfile | grep activesupport
gem 'activesupport'

$ bundle show activesupport
/Users/2b-software-mac/.rvm/gems/ruby-3.1.2/gems/activesupport-7.0.6

$ bundle exec ruby my_ruby_script.rb
Can I find the gem?
#<Bundler::StubSpecification name=activesupport version=7.0.6 platform=ruby>
Can I require the gem?
my_ruby_script.rb:4:in `require': cannot load such file -- activesupport (LoadError)
    from my_ruby_script.rb:4:in `<main>'

脚本

puts 'Can I find the gem?'
puts Gem::Specification.find_all{ |g| g.name.include? 'activesupport' }
puts 'Can I require the gem?'
require 'activesupport'

版本

  • RVM 版本 1.29.12
  • 红宝石 3.1.2p20
  • 创业板 3.3.7
  • 捆绑包 2.3.7
  • macOS 13.5.2 (M2)
RubyGems 捆绑器 RVM Ruby-3

评论


答:

1赞 spickermann 9/21/2023 #1

gem 必须像这样加载(注意下划线):

require "active_support"

阅读 Rails 指南中有关独立主动支持的信息。

评论

0赞 Dominus.Vobiscum 9/21/2023
我真是个傻瓜!感谢您的帮助。👍🏻