Bundle exec を使用して Ruby 3 (rvm を使用) でスクリプトを実行すると LoadError が発生する
概要
次の Ruby スクリプトでは、activesupport gem が必要な場合に LoadError が発生しますが、それ以外はインストールされていることが示されています。 Bundle exec で実行しても、rvm 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'
解決策
gem は次のようにロードする必要があります (アンダースコアに注意してください)。
require "active_support"
Rails ガイドでスタンドアロン アクティブ サポートについてお読みください。