.xonshrc でコマンドのパス通す設定を書くときは末尾への追加をする append だけじゃなくて insert(0, "任意のPATH") とかも使っていこうねという至極当たり前のお話

f:id:bps_tomoya:20181229141901j:plain

Homebrew でシュッ入れてパス通したつもりの Ruby を認識しなくてアレッとなったのでメモ。
2019年1月11日時点でインストールされるのは Ruby 2.6.0 である。

$ brew install ruby

==> Downloading https://homebrew.bintray.com/bottles/ruby-2.6.0.mojave.bottle.ta
Already downloaded: /Users/tomoya_shibata/Library/Caches/Homebrew/downloads/cd60e4a4a55d9f91ebcfa3eb10a558ed019af8ad08e1e19b722fb146bd9a121b--ruby-2.6.0.mojave.bottle.tar.gz
==> Pouring ruby-2.6.0.mojave.bottle.tar.gz
==> Caveats
By default, binaries installed by gem will be placed into:
  /usr/local/lib/ruby/gems/2.6.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH run:
  echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile

For compilers to find ruby you may need to set:



For pkg-config to find ruby you may need to set:


==> Summary
🍺  /usr/local/Cellar/ruby/2.6.0: 19,334 files, 32.3MB

xonsh で叩きたいのでパスを通すために .xonshrc に以下を追記した。

$PATH.append("/usr/local/opt/ruby/bin")

あらかじめ言っておくとこれは正しくない。
システム側の Ruby が優先されたままになってしまうのだ。
$PATH を確認するとこんな順番に宣言されている。

$ $PATH
EnvPath(
['/usr/bin',
 '/bin',
 '/usr/sbin',
 '/sbin',
 '/usr/local/bin',
 '/usr/local/opt/ruby/bin'] # append だから末尾に追加される
)

macOS のシステムにインストールされている Ruby/usr/bin に配置されているので、順番の若いこちらが優先的に参照されてしまう。
今回の場合、末尾ではなく先頭に宣言されていると都合がよいので append ではなく insert を使う。

docs.python.jp

# 先頭に挿入したいので位置を指定する第1引数には 0を渡す
$PATH.insert(0, "/usr/local/opt/ruby/bin")

これで $PATH の中身が期待した順番になる。

$ $PATH
EnvPath(
['/usr/local/opt/ruby/bin',
 '/usr/bin',
 '/bin',
 '/usr/sbin',
 '/sbin',
 '/usr/local/bin']
)

Homebrew でインストールした Ruby が優先して参照されるようになった。

$ ruby --version
ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin18]

めでたし。