Jacob Rodrigues
Jacob Rodrigues

Reputation: 3

How to use require in Ruby?

I'm having trouble using the require in Ruby when I try to call the animal object.

#Classe Animal in file call animal.rb
class Animal
    def pular
        puts 'Toing! tóim! bóim! póim!'
    end

    def dormir
        puts 'ZzZzZ!'
    end
end

# Require rying to call the class Animal with arquivo app.rb
require './animal.rb'

animal = Animal.new

animal.pular

Traceback (most recent call last):
app.rb:3:in '': uninitialized constant Animal (NameError)

Upvotes: 0

Views: 179

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

You don't specify the ".rb" as Ruby will search for matching ".rb" (or ".dll" or whatever)

Also the file is in the same directory, so you would use require_relative

require_relative 'animal'

Upvotes: 2

Related Questions