【问题标题】:Ruby modules and classes not working togetherRuby 模块和类不能一起工作
【发布时间】:2018-08-17 07:36:44
【问题描述】:

我有一个模块Animal

module Animal
  def sit
    puts '*sitting*'
  end
end

我是includingDog 中的那个模块。

class Dog
  include Animal
end

dog = Dog.new.sit

运行时,我得到这个错误:

dog.rb:2:in <class:Dog>':
uninitialized constant Dog::Animal (NameError)  from
dog.rb:1:in <main>

我做错了什么?

【问题讨论】:

标签: ruby


【解决方案1】:

我猜测一些可能性:

A.您在不同的文件中有 Animal 正文和 Dog 正文,并从后者调用前者而不加载或需要文件

B. Dog 定义后跟 Animal 定义。

如果是这种情况,则解决它。

【讨论】:

    猜你喜欢
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多