当我运行以下代码时,我收到的错误是“fig_match:rb:5:in'':未初始化的常量Match :: Fig(NameError)来自fig_match.rb:4:in”
我正在测试我的设置和战斗方法,这就是我在Match类中的变量setter之后进行设置和匹配调用的原因。
require_relative = 'fig_user.rb' #class name is Fig within fig_user.rb
class Match
fig1 = Fig.new
fig2 = Fig.new
go = 0
winner = nil
setup(Bob, Sam)
match.battle
def setup(name1, name2)
#set names
@name1 = fig1.name
@name2 = fig2.name
go = rand(2)
end
def battle
if go.even?
p fig1.name
end
end
端
这是上面代码中引用的单独类(不确定是否重要)
class Fig
attr_reader :name, :power, :health
attr_accessor :name, :power, :health
deckId = @id
name = @name
power = @power
moves = Hash["Kick", 50, "Punch", 30]
health = 100
end
答案 0 :(得分:2)
require_relative
是一种方法。您已将其指定为变量名称。因此,您的范围内没有Fig类。