0

다음 코드를 실행하면 "fig_match : rb : 5 : in": 초기화되지 않은 상수 인 Match :: Fig (NameError)가 fig_match.rb : 4 :에서 "초기화되지 않은 상수 오류

필자는 설정 및 전투 방법을 테스트하는 중이었기 때문에 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 

답변

2

require_relative는 방법이다. 변수 이름으로 지정했습니다. 그러므로, 당신은 당신의 범위에 어떤 FIG 클래스도 가지고 있지 않습니다.

+1

감사합니다. 방금 require_relative의 위치를 ​​변경하려고했습니다. 내 범위에 들어가려면 어떻게 될까요? (새로운 루비와 프로그래밍) – Dru

+0

+1,'require_relative = 'fig_user.rb''는'require_relative'여야합니다. fig_user.rb'' –

+0

고마워요. 그래서 작동합니다. – Dru

관련 문제