当我尝试调用动物对象时,在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)
答案 0 :(得分:2)
您未指定“ .rb”,因为Ruby会搜索匹配的“ .rb”(或“ .dll”或其他)
该文件也位于同一目录中,因此您将使用require_relative
require_relative 'animal'