valid,invalid

関心を持てる事柄について

macOSでirbのhistoryが使えなくなったときの対処法

あるmacOSでいつの間にか、irbhistoryが使えなくなっていた。使えないとは、irbで矢印キーの↑を押すと制御文字 ^[[A が表示され、ENTERを押すとirbプロセスが終了するような状態。

$ irb
irb(main):001:0> RUBY_VERSION
=> "2.6.0"
irb(main):002:0> ^[[A

$ 

Ruby versionは異なるが、macos - IRB history not working with Ruby 2.3.0 - Stack Overflowに従って再インストール(再ビルド)することで解消した。

rbenv automatically detects homebrew and looks in it for readline, so, if you're using Homebrew and irb history doesn't work, you either haven't installed readline or you built your Ruby before you installed readline.

  • brew install readline if it isn't installed already
  • rbenv uninstall 2.3.0
  • rbenv install 2.3.0

なぜこれで直るのか?

StackOverflowの解説を読んでへーと思ったのでメモ。

OS X's command-line editing is based on the libedit library. OS X has a version of the readline library which is a wrapper around libedit, but it does not behave completely like GNU readline. irb history works in Ruby built with OS X's wrapper up to Ruby 2.1, but Ruby 2.2 and later need to be built with GNU readline for irb history to work.

  • OS Xコマンドライン編集ではlibeditのラッパーを使っているがGNU readlineとの完全な互換性はない
  • Ruby2.1まではそれで動いていたが、2.2以降はGNU readlineとともにビルドする必要がある
  • Rubyのビルド時にhomebrewでインストールされたreadlineを検出して使用するようになった ref

readlineとは?

以下の記事に詳しい。

readline は、CUI アプリケーションにおいてユーザが行を入力する際に便利な「行頭・行末移動」「ヒストリ機能」などを提供するライブラリである。readline というコマンドがあるわけではない。

readlineコマンドの使い方: UNIX/Linuxの部屋より引用)

そもそもlibeditとは?

知らなかったのでググった。

readline のライセンスが GPL であることを嫌い、NetBSD 界隈で readline 互換の libedit というライブラリが開発された。editline と表記することもある。

libedit は GPL ではなく BSD ライセンスとなっている。readline との互換性 100% が目標だが、libedit 独自の機能拡張もある。

readlineコマンドの使い方: UNIX/Linuxの部屋 より引用)

とのこと。ややこしい。

macOSのlibeditに関する問題点は他にもいろいろあるようだ。

環境