运行Ruby gem时出错但本地目录工作正常

时间:2018-02-12 02:44:25

标签: ruby rubygems

我构建了一个名为“Knitpickr”的gem,并将其发布到RubyGems.org。当我在其本地目录中运行它时程序运行正常;但是,当我安装gem并运行它时,我收到以下错误消息:

/usr/local/rvm/gems/ruby-2.3.1/gems/knitpickr-0.1.8/lib/knitpickr/controller.rb
:1:in `<top (required)>': uninitialized constant Knitpickr (NameError)
        from /usr/local/rvm/gems/ruby-2.3.1/gems/knitpickr0.1.8/lib/knitpickr.
rb:4:in `require_relative'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/knitpickr-0.1.8/lib/knitpickr.
rb:4:in `<top (required)>'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/knitpickr-0.1.8/bin/knitpickr:
3:in `require_relative'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/knitpickr-0.1.8/bin/knitpickr:
3:in `<top (required)>'
        from /usr/local/rvm/gems/ruby-2.3.1/bin/knitpickr:23:in `load'
        from /usr/local/rvm/gems/ruby-2.3.1/bin/knitpickr:23:in `<main>'
        from /usr/local/rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `ev
al'
        from /usr/local/rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<m
ain>'

我认为由于某种原因,它无法识别Knitpickr模块命名空间。以下是错误所指的代码:

class Knitpickr::Controller

我有一个gemspec文件集(或者至少据我所知)从lib / knitpickr.rb文件运行,其中包括以下内容:

require 'open-uri'
require 'nokogiri'

require_relative './knitpickr/controller'
require_relative './knitpickr/scraper'
require_relative './knitpickr/version'
require_relative './knitpickr/yarn'

module Knitpickr
end

模块在那里定义,所以我很困惑为什么“Knitpickr”在controller.rb文件中被视为未初始化的常量。

让我感到困惑的是,为什么程序在本地目录中正常运行,但在运行gem时遇到错误。

可以使用

安装gem
gem install knitpickr

任何人都知道发生了什么事?

1 个答案:

答案 0 :(得分:0)

如果查看跟踪,knitpickr在它甚至初始化Knitpickr模块之前就失败了。在初始化模块之前,您需要控制器。

将模块初始化移动到需要亲属之上。

require 'open-uri'
require 'nokogiri'

module Knitpickr
end

require_relative './knitpickr/controller'
require_relative './knitpickr/scraper'
require_relative './knitpickr/version'
require_relative './knitpickr/yarn'