-1

I have a module Animal:

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

and I am including that module in a class Dog.

class Dog
  include Animal
end

dog = Dog.new.sit

When running this, I get this error:

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

What am I doing wrong?

sawa
  • 165,429
  • 45
  • 277
  • 381
Trenton Tyler
  • 1,692
  • 3
  • 24
  • 53
  • Possible duplicate of [Inheriting class methods from modules / mixins in Ruby](https://stackoverflow.com/questions/10692961/inheriting-class-methods-from-modules-mixins-in-ruby) – neznidalibor Mar 08 '18 at 20:51
  • Not a duplicate. Just a simple constant lookup error. The `Animal` constant needs to be visible to the `Dog` class. It needs to be defined first or defined in the same file using `require`. – Max Mar 08 '18 at 22:16

1 Answers1

1

I am guessing some possibilities:

A. You have the Animal body and Dog body in different files, and calling the former from the latter without loading or requiring the file

B. You the Dog definition followed by the Animal definition.

If either is the case, then resolve it.

sawa
  • 165,429
  • 45
  • 277
  • 381