【问题标题】:uninitialized constant (NameError) Ruby未初始化的常量 (NameError) Ruby
【发布时间】:2020-01-09 15:36:53
【问题描述】:

我对 ruby​​ 还很陌生,正在尝试编写 gem 文件,但出现以下错误。我已按照其他帖子中的建议更新了 bundler 和相关的 gem。 与

一起开发
  • ubuntu 18.04LTS
  • 红宝石 2.5.1p57
  • 宝石 3.1.2
Traceback (most recent call last):
    15: from /usr/local/bin/vcdm:23:in `<main>'
    14: from /usr/local/bin/vcdm:23:in `load'
    13: from /var/lib/gems/2.5.0/gems/vcdm-0.1.2/bin/vcdm:4:in `<top (required)>'
    12: from /usr/local/lib/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:72:in `require'
    11: from /usr/local/lib/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:72:in `require'
    10: from /var/lib/gems/2.5.0/gems/vcdm-0.1.2/lib/vcdm.rb:2:in `<top (required)>'
     9: from /usr/local/lib/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:72:in `require'
     8: from /usr/local/lib/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:72:in `require'
     7: from /var/lib/gems/2.5.0/gems/vcdm-0.1.2/lib/vcdm/command.rb:1:in `<top (required)>'
     6: from /var/lib/gems/2.5.0/gems/vcdm-0.1.2/lib/vcdm/command.rb:4:in `<module:Vcdm>'
     5: from /var/lib/gems/2.5.0/gems/vcdm-0.1.2/lib/vcdm/command.rb:4:in `glob'
     4: from /usr/local/lib/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:92:in `require'
     3: from /usr/local/lib/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:92:in `require'
     2: from /var/lib/gems/2.5.0/gems/vcdm-0.1.2/lib/vcdm/commands/hostfile.rb:4:in `<top (required)>'
     1: from /var/lib/gems/2.5.0/gems/vcdm-0.1.2/lib/vcdm/commands/hostfile.rb:5:in `<module:Vcdm>'
/var/lib/gems/2.5.0/gems/vcdm-0.1.2/lib/vcdm/commands/hostfile.rb:14:in `<class:HostfileCommand>': uninitialized constant Vcdm::HostfileCommand::CommandOption (NameError)

这些是我想要执行的代码 command_option.rb

module Vcdm

  class CommandOption
    attr_reader :name, :type, :description

    def initialize(name, type, description)
      @name = name
      @type = type
      @description = description
    end

  end

end

hostfile.rb

require 'vcdm/command_interface'

module Vcdm
  class HostfileCommand
    as = CommandOption.new("--path STRING", String, "custom hosts path")

    IS_PUBLIC_COMMAND = true
    SYNTAX = 'vcdm hostfile'
    SUMMARY = 'adds the ingress url to the users hostfile'
    DESCRIPTION = ""
    EXAMPLE = "vcdm hostfile --path=/mnt/c/Windows/System32/drivers/etc/hosts"
    EXAMPLE_DESCRIPTION = ""
    implements CommandInterface

  end

end

command_interface.rb

require 'class_interface'

class CommandInterface
  IS_PUBLIC_COMMAND = true | false
  SYNTAX = String
  SUMMARY = String
  DESCRIPTION = String
  EXAMPLE = String
  EXAMPLE_DESCRIPTION = String
  OPTIONS = Array

  def initialize
  end

  def execute(args, options)
  end

end

有什么问题吗?

【问题讨论】:

  • 假设 CommandOption 为:Vcdm::HostfileCommand::CommandOption,尝试将 hostfile.rb 第 5 行中的 CommandOption 更改为 Vcdm::CommandOption。

标签: ruby-on-rails ruby rubygems


【解决方案1】:

感觉你好像少了一个

require 'vcdm/command_option'

在您的hostfile.rb 中。只需将该行添加到该文件的顶部即可。

【讨论】:

    【解决方案2】:

    尝试将其放入 'config/application.rb'

    config.eager_load_paths << "#{Rails.root}/lib"
    

    然后在'hostfile.rb'中

    require 'vcdm/command_interface'
    
    module Vcdm
      include CommandOption
      class HostfileCommand
      ...
    

    【讨论】:

    • 但是我没有任何 config/application.rb
    • 我明白了...然后尝试添加包含。
    • 得到了uninitialized constant Vcdm::HostfileCommand (NameError)
    • 等一下,我的错...你能在 HostfileCommand 类之上“包含 CommandOption”吗
    • 同样的错误uninitialized constant Vcdm::CommandOption (NameError)
    猜你喜欢
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多