找不到运行“ruby.rb”的 gem –但在IRB工作
当我尝试使用 Ruby 命令运行 .rb 文件并尝试访问 gem 时,我遇到了问题。我尝试使用的 gem 是 Ruby-Whois。我下面有一个示例脚本,当我尝试通过“ruby whois.rb”执行它时,我收到此错误消息:
./whois.rb:6: uninitializedconstant Whois (NameError)
但是,如果我运行相同的脚本行在 IRB 中我得到了预期的结果。可能是什么原因造成的?
下面是 whois.rb
require "rubygems"
require "whois"
domain = "google.com"
c = Whois::Client.new
a = c.query(domain)
puts a
I am experiencing issues when I am trying to run my .rb-file with the Ruby-command trying to access a gem. The gem i am trying to use is Ruby-Whois. I have an example script below that when I try to execute it through "ruby whois.rb" I get this error message:
./whois.rb:6: uninitialized constant Whois (NameError)
However, if I run the same script line by line in IRB I get the expected result. What may cause this?
Below is whois.rb
require "rubygems"
require "whois"
domain = "google.com"
c = Whois::Client.new
a = c.query(domain)
puts a
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改文件名 -
require 'whois'
中存在歧义,并且 ruby 需要您的文件而不是 gem。当你在 IRB 中逐行执行此操作时,Ruby 知道你到底想要什么,所以一切正常。change the name of your file - there is ambiguity in
require 'whois'
and ruby is requireing your file instead of a gem. when you do it line by line in irb ruby knows what you exactly want to require, so everything works.