我有一个简单的ruby脚本,它使用我net-ping
安装的gem install net-ping
gem,并确认已安装:
Successfully installed net-ping-1.7.8
Parsing documentation for net-ping-1.7.8
Done installing documentation for net-ping after 0 seconds
1 gem installed
在我的脚本中: 要求' net / ping'
def can_ping(host)
@icmp = Net::Ping::ICMP.new(host)
if @icmp.ping
can_ping = true
else
can_ping = false
end
end
can_ping(www.google.com)
在irb中这一切都运行正常,但是在尝试运行我的脚本时,它并没有。相反,我现在得到这个错误:
/Users/golem/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- net/ping (LoadError)
from /Users/golem/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from checkin.rb:2:in `<main>'
这曾经在我的旧版本脚本上运行良好,但是,我已经在我的Mac上安装了Oh-My-Zsh
,我怀疑这是问题。
所以,我确保我的环境变量引用了RVM(printenv
):
rvm_prefix=/Users/golem
rvm_path=/Users/golem/.rvm
rvm_bin_path=/Users/golem/.rvm/bin
我是否还有其他可能造成这种情况的原因?
答案 0 :(得分:2)
因此,net ping需要root权限才能运行。因此,如果您使用的是sudo,则需要再次安装gem。所以,首先执行sudo su
,再次运行gem install net-ping:1.7.8
并尝试运行脚本。
这应该有效。