未初始化常量错误

发布于 2024-11-18 13:16:21 字数 832 浏览 1 评论 0原文

当我运行以下代码时,出现错误“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2024-11-25 13:16:21

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文