未初始化常量错误
当我运行以下代码时,出现错误“fig_match:rb:5:in '': uninitializedconstant Match::Fig (NameError) from Fig_match.rb:4:in”
我正在测试中我的设置和战斗方法,这就是为什么我在 Match 类中的变量设置器之后进行设置和匹配调用。
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
When I run the following code I'm getting an error that reads "fig_match:rb:5:in '': uninitialized constant Match::Fig (NameError) from fig_match.rb:4:in"
I was in the midst of testing my setup and battle methods, which is why I have the setup and match calls after my variable setters in the Match class.
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
end
This is the separate class that's being referenced in the above code (not sure if it matters)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
require_relative
是一种方法。您已将其指定为变量名称。因此,您的范围内没有 Fig 类。require_relative
is a method. You've assigned it as a variable name. Hence, you have no Fig class in your scope.