错过了一些基本的红宝石概念

时间:2015-04-07 23:58:29

标签: ruby module

我有一个具有以下结构的ruby项目

(root)
-bin/
-lib/
-specs/
  - base.rb
  - cli.rb
  - settings.rb

cli.rb中的代码具有以下内容,

此文件包含一系列在文件中使用的必需stmts

module X
  class cli
    #some-code
  end
end

base.rb中的代码有一行我无法理解,

module X
  class Base
    def setup
     @cli = Cli.new.global --> I don't understand this line as there is no require statement above that.
     # more code
    end
  end
end

我可能在这里缺少一些基本概念,因为我对ruby很新。

由于

在代码中添加一行让我对这个问题感到疑惑,

https://github.com/agent462/sensu-cli/blob/master/lib/sensu-cli/base.rb#L4

1 个答案:

答案 0 :(得分:0)

我最初的假设是错误导致混淆,所以我改变了整个答案。

因为所谓的未定义标识符(Cli)位于方法定义(X::base#setup)内,所以在加载代码时不会解析它。只有在调用方法时才会解析它。这允许文件包含base.rb而没有定义Cli。唯一出现真正问题的是调用setup并且未定义Cli