未初始化的常量 (NameError) Ruby

uninitialized constant (NameError) Ruby

我是 ruby 的新手,正在尝试编写 gem 文件,但出现以下错误 occurs.I 已按照建议更新了捆绑器和相关的 gem在其他帖子中。 使用

开发
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

有什么问题吗?

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

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

然后在 'hostfile.rb'

require 'vcdm/command_interface'

module Vcdm
  include CommandOption
  class HostfileCommand
  ...

我觉得你少了一个

require 'vcdm/command_option'

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