高木のブログ

Ruby 2.6からirbはGem化されたので、Gemfileに記述しないと「bundle exec irb」は使えない

2021/10/01

タイトルがすべて

Ruby 2.5までのirbは標準ライブラリだったが、Ruby 2.6でGem化されたらしい

プレーンなRubyプロジェクトで「bundle exec irb」が起動できなくてびっくりした
Ruby 2.7も出て、もうRuby 3系も出ているのでかなり今更だけど

$ bundle exec irb
bundler: failed to load command: irb (/Users/ytkg/.rbenv/versions/2.7.4/bin/irb)
Traceback (most recent call last):
        16: from /Users/ytkg/.rbenv/versions/2.7.4/bin/bundle:23:in `<main>'
        15: from /Users/ytkg/.rbenv/versions/2.7.4/bin/bundle:23:in `load'
        14: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/exe/bundle:37:in `<top (required)>'
        13: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/lib/bundler/friendly_errors.rb:128:in `with_friendly_errors'
        12: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/exe/bundle:49:in `block in <top (required)>'
        11: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/lib/bundler/cli.rb:25:in `start'
        10: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
         9: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/lib/bundler/cli.rb:31:in `dispatch'
         8: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
         7: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
         6: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
         5: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/lib/bundler/cli.rb:478:in `exec'
         4: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/lib/bundler/cli/exec.rb:23:in `run'
         3: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/lib/bundler/cli/exec.rb:58:in `kernel_load'
         2: from /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bundler-2.2.28/lib/bundler/cli/exec.rb:58:in `load'
         1: from /Users/ytkg/.rbenv/versions/2.7.4/bin/irb:23:in `<top (required)>'
/Users/ytkg/.rbenv/versions/2.7.4/bin/irb:23:in `load': cannot load such file -- /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/specifications/default/exe/irb (LoadError)

使えるようにする方法

Gemfileに追加すればよい

Gemfile
gem "irb", require: false
$ bundle install
$ bundle exec irb
irb(main):001:0>

もちろん素のirbも起動できない

Gemをインストールすれば良い

$ irb
Traceback (most recent call last):
        1: from /Users/ytkg/.rbenv/versions/2.7.4/bin/irb:23:in `<main>'
/Users/ytkg/.rbenv/versions/2.7.4/bin/irb:23:in `load': cannot load such file -- /Users/ytkg/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb (LoadError)
$ gem install irb
$ irb
irb(main):001:0>

参考


SNS でシェアする


ytkg

Written by ytkg, Twitter, GitHub