如何在Ruby中使用require?

时间:2019-05-25 16:01:41

标签: ruby

当我尝试调用动物对象时,在Ruby中使用require遇到了麻烦。

#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)

1 个答案:

答案 0 :(得分:2)

您未指定“ .rb”,因为Ruby会搜索匹配的“ .rb”(或“ .dll”或其他)

该文件也位于同一目录中,因此您将使用require_relative

require_relative 'animal'