簡體   English   中英

未初始化的常量 (NameError) Ruby

[英]uninitialized constant (NameError) Ruby

我對 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

主機文件

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

命令接口.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

有什么不對的嗎?

我覺得你好像錯過了一個

require 'vcdm/command_option'

在您的hostfile.rb 只需將該行添加到該文件的頂部。

嘗試將其放在“config/application.rb”中

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

然后在'hostfile.rb'

require 'vcdm/command_interface'

module Vcdm
  include CommandOption
  class HostfileCommand
  ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM