Ruby 'requires' are throwing NoMethodError

2019-08-27 22:21发布

问题:

Trying to run a ruby script, but it's failing on the 'require' statements, throwing a strange error:

Code:

require "rubygems"
require "bundler/setup"
require_relative "plugins/pivotal_rabbitmq_plugin/pivotal_rabbitmq_plugin.rb"
require "newrelic_plugin"

module PivotalAgent
  NewRelic::Plugin::Config.config_file=File.dirname(__FILE__) + "/config/newrelic_plugin.yml"
  NewRelic::Plugin::Run.setup_and_run
end

Command:

ruby pivotal_agent.rb

Error:

/usr/share/rubygems/rubygems/path_support.rb:78:in `path=': undefined method `+' for nil:NilClass (NoMethodError)
        from /usr/share/rubygems/rubygems/path_support.rb:34:in `initialize'
        from /usr/share/rubygems/rubygems.rb:325:in `new'
        from /usr/share/rubygems/rubygems.rb:325:in `paths'
        from /usr/share/rubygems/rubygems.rb:348:in `path'
        from /usr/share/rubygems/rubygems/specification.rb:872:in `dirs'
        from /usr/share/rubygems/rubygems/specification.rb:750:in `stubs'
        from /usr/share/rubygems/rubygems/specification.rb:938:in `find_inactive_by_path'
        from /usr/share/rubygems/rubygems.rb:186:in `try_activate'
        from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:126:in `rescue in require'
        from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:39:in `require'
        from pivotal_agent.rb:2:in `<main>'

Any ideas what might be causing that? My initial hunch is that it has something to do with the file paths it's looking at being formatted for Unix while this is running on a Windows machine, but I've added Ruby to PATH, so I don't know why it would be looking there rather than where Ruby is actually installed (C:\Ruby22-x64).

EDIT: To clarify, the first require (for 'rubygems') doesn't throw an error, but both the other requires and the require_relative all throw a similar one.

回答1:

Not 100% sure why, but removing the 'ruby' from the start of the command fixed it. So just running

my_script.rb

works fine, but running

ruby my_script.rb

gives the above stack.