在Homebrew中安装Ruby后,如何让irb
工作?
当我尝试运行irb
时,我得到一个错误:
$ irb
Traceback (most recent call last):
2: from /usr/local/opt/ruby/bin/irb:23:in `<main>'
1: from /usr/local/lib/ruby/site_ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
/usr/local/lib/ruby/site_ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': can't find gem irb (>= 0.a) with executable irb (Gem::GemNotFoundException)
我试过了:
$ brew link ruby
Warning: Refusing to link macOS-provided software: ruby
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:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
我在/etc/paths
文件的顶部有以下几行:
/usr/local/bin
/usr/local/opt/ruby/bin
/usr/local/lib/ruby/gems/2.6.0/bin
irb
没有出现在gem list
的输出中,但:
$ find /usr/local -name irb
/usr/local/lib/ruby/2.6.0/irb
/usr/local/Cellar/ruby/2.6.0_1/bin/irb
/usr/local/Cellar/ruby/2.6.0_1/lib/ruby/2.6.0/irb
/usr/local/Cellar/ruby/2.6.0_1/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb
/usr/local/Cellar/ruby/2.6.0_1/share/ri/2.6.0/system/lib/irb
我对ri
&rdoc
。
运行:gem install irb
,现在就可以开始了。
假设您使用的是Homebrew Ruby。。。
irb
可执行文件位于:
/usr/local/opt/ruby/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb
您可以直接使用该行,将其符号链接到您的$PATH
、alias
it或其他任何内容中。
或者,您可以在第22行周围修补/usr/local/opt/ruby/bin/irb
。
# patch
class Gem::BasicSpecification
def self.default_specifications_dir
File.join(Gem.private_dir, "specifications", "default")
end
end
# /patch
# Next line looks like this. Don't change this.
# if Gem.respond_to?(:activate_bin_path)
您也可以在/usr/local/opt/ruby/bin/ri
和/usr/local/opt/ruby/bin/rdoc
中执行同样的操作来修补这些命令。
为什么?
请参阅https://github.com/Homebrew/homebrew-core/blob/955497722b9bf65069957b0e7c903b96939cdd99/Formula/ruby.rb#L112
Homebrew Ruby公式假定所有gem都将安装在"全局gem目录"/usr/local/lib/ruby/gems/2.6.0/
中。因此,当你卸载并重新安装Homebrew Ruby时,gems会一直存在——你也不必重新安装它们(有点烦人,因为我为Ruby版本安装了gems,我甚至还没有安装,但这是另一个问题)。
但是Ruby的默认gem不在全局gem目录中。它们在Ruby安装目录(Homebrew公式称之为private_dir
):/usr/local/opt/ruby/lib/ruby/gems/2.6.0/
中。
所以自制红宝石找不到它们。
Homebrew对Rubygems进行了修补,所以这个片段再次对Rubygims进行修补,但更深入。你也可以这样修补补丁:
module Gem
def self.default_dir
private_dir
end
end
但default_dir
在其他地方使用,我不想破坏任何东西。