【问题标题】:Why do I get a "method undefined" error for this method?为什么我会收到此方法的“方法未定义”错误?
【发布时间】:2014-11-28 01:25:07
【问题描述】:

我在调用方法启动之前定义了它,但我仍然被告知它尚未定义。我试过寻找答案,但我能找到的唯一信息说该方法需要在调用之前定义,我显然已经完成了!代码如下,启动在底部被调用。我知道我的代码不完整,我就是无法克服这个未定义的错误。

# Ruby code file - All your code should be located between the comments provided.

# Add any additional gems and global variables here
# require 'sinatra'     # remove '#' character to run sinatra wen server

# Main class module
module OXs_Game
# Input and output constants processed by subprocesses. MUST NOT change.
NOUGHT = 0
CROSS = 1

class Game
    attr_reader :matrix, :input, :output, :player1, :player2, :winner
    attr_writer :matrix, :input, :output, :player1, :player2, :winner

    def initialize(input, output)
        @input = input
        @output = output
    end

    # Any code/methods aimed at passing the RSpect tests should be added below.
        def start
            @output.puts "Welcome to Noughts and Crosses!"
            @output.puts "Starting game..."
            @output.puts "Created by:Stephen Mitchell"
            @output.puts "Player 1: 0 and Player 2: 1"
        end

        def created_by
            return "myname"
        end

        def student_id
            return numberhere
        end

        def setplayer1
            @player1 = 0
        end

        def setplayer2
            @player2 = 1
        end

        def clearmatrix
            @matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
        end

        def getmatrixvalue(n)
            @matrix[n]
        end

        def setmatrixvalue(i, v)
            @i = 1
            @v = "0"
            @matrix[i] = "0"            
        end

        def displaykey(matrix)
            @matrix = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
            @output.puts "Table key:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
        end

        def displaymatrix
            @matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
            @output.puts "Table status:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
        end

        def finish
            @output.puts "Finishing game..."
        end

        def displaymenu
            @output.puts "Menu: (1)Start | (2)New | (9)Exit\n"
        end

        def checkwinner
            if @matrix[0] = "0" && @matrix[1] = "0" && @matrix[2] = "0" then
                winner = 1
            elsif @matrix[3] = "0" && @matrix[4] = "0" && @matrix[5] = "0" then
                winner = 1
            elsif @matrix[6] = "0" && @matrix[7] = "0" && @matrix[8] = "0" then
                winner = 1
            elsif @matrix[0] = "0" && @matrix[3] = "0" && @matrix[6] = "0" then
                winner = 1
            elsif @matrix[1] = "0" && @matrix[4] = "0" && @matrix[7] = "0" then
                winner = 1  
            elsif @matrix[2] = "0" && @matrix[5] = "0" && @matrix[8] = "0" then
                winner = 1
            elsif @matrix[0] = "0" && @matrix[4] = "0" && @matrix[8] = "0" then
                winner = 1
            elsif @matrix[2] = "0" && @matrix[4] = "0" && @matrix[6] = "0" then
                winner = 1
            elsif @matrix[0] = "1" && @matrix[1] = "1" && @matrix[2] = "1" then
                winner = 2
            elsif @matrix[3] = "1" && @matrix[4] = "1" && @matrix[5] = "1" then
                winner = 2
            elsif @matrix[6] = "1" && @matrix[7] = "1" && @matrix[8] = "1" then
                winner = 2
            elsif @matrix[0] = "1" && @matrix[3] = "1" && @matrix[6] = "1" then
                winner = 2
            elsif @matrix[1] = "1" && @matrix[4] = "1" && @matrix[7] = "1" then
                winner = 2  
            elsif @matrix[2] = "1" && @matrix[5] = "1" && @matrix[8] = "1" then
                winner = 2
            elsif @matrix[0] = "1" && @matrix[4] = "1" && @matrix[8] = "1" then
                winner = 2
            elsif @matrix[2] = "1" && @matrix[4] = "1" && @matrix[6] = "1" then
                winner = 2  
            end
        end

    # Any code/methods aimed at passing the RSpect tests should be added above.

end
end

# Main program
module OXs_Game
@input = STDIN
@output = STDOUT
g = Game.new(@input, @output)
matrixkey = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
playing = true
input = ""
option = 0
turn = 0

# Any code added to output the activity messages to the command line window should be added below.

def startup
    g.start
    g.displaykey(matrixkey)
    g.displaymatrix
    puts "Please select an option:"
    g.displaymenu
    menu_input = gets.chomp
end

def menu
    if menu_input == 1 then
        player1turn
    elsif menu_input == 2 then
        g.clearmatrix
        startup
    elsif menu_input == 9 then
        exit
    end
end

def player1turn
    g.displaymatrix
    puts "Player 1 to move."
    player1input = gets.chomp
    if player1input == 1 then
        @matrix[0] == "0"
    elsif player1input == 2 then
        @matrix[1] == "0"
    elsif player1input == 3 then
        @matrix[2] == "0"
    elsif player1input == 4 then
        @matrix[3] == "0"
    elsif player1input == 5 then
        @matrix[4] == "0"
    elsif player1input == 6 then
        @matrix[5] == "0"
    elsif player1input == 7 then
        @matrix[6] == "0"   
    elsif player1input == 8 then
        @matrix[7] == "0"   
    elsif player1input == 9 then
        @matrix[8] == "0"
    else
        puts "Invalid input, please try again."
        player1turn
    end

end 

startup 
# Any code added to output the activity messages to the command line window should be added above.

结束

【问题讨论】:

  • 错误出现在哪里?不要指望人们会筛选所有代码...
  • 它发生在调用启动时,在代码的末尾。就像我在帖子中所说的那样。
  • 请求帮助调试问题的问题需要演示问题所需的最少代码。你有没有把它剥离到那个地步?很有可能将其剥离的过程会向您显示问题。
  • 几个问题: 1. 方法student_id 中的numberhere 是未定义的局部变量或未定义的方法。 2.局部变量matrixkeystartup中不可见。

标签: ruby methods undefined


【解决方案1】:

看起来您已经将startup 方法定义为OXs_Game 模块的一部分的实例方法,但是当您调用它时,您就在模块之外。

尝试删除您的OXs_Game 模块(但将其中定义的所有方法保留在代码中)。

如果您保留 OXs_Game 模块,您的代码将会遇到其他问题,因为您在模块定​​义中使用局部变量,但是每当您使用 def 定义一个方法时,它会创建一个新范围,您将无法再访问局部变量。使用class 定义类或module 定义模块时也是如此。

【讨论】:

    【解决方案2】:

    跳出来的一件事是:

    def setmatrixvalue(i, v)
        @i = 1
        @v = "0"
        @matrix[i] = "0"            
    end
    

    你为什么不对v做点什么?为什么不调用该方法?

    【讨论】:

      【解决方案3】:

      这是工作代码。 我做了两个主要的改变。 在主程序中,您声明了在 startupmenu 方法中无法访问的局部变量,因此我将它们更改为实例变量。

      其次,您的 startup 方法是实例方法,不能通过调用 startup 直接调用,如果您想以这种方式调用它,则需要通过 OXs_Game::startup 调用它。在这个 i 代码中,我将 startup 方法更改为类级别方法,因此可以直接调用它。您可以通过OXs_Game::startup 调用它,并将其保留为您的代码中的实例级别:)

      module OXs_Game
       #Input and output constants processed by subprocesses. MUST NOT change.
      NOUGHT = 0
      CROSS = 1
      class Game
          attr_reader :matrix, :input, :output, :player1, :player2, :winner
          attr_writer :matrix, :input, :output, :player1, :player2, :winner
          def initialize(input, output)
              @input = input
              @output = output
          end
      
      #Any code/methods aimed at passing the RSpect tests should be added below.
          def start
              @output.puts "Welcome to Noughts and Crosses!"
              @output.puts "Starting game..."
              @output.puts "Created by:Stephen Mitchell"
              @output.puts "Player 1: 0 and Player 2: 1"
          end
      
          def created_by
              return "myname"
          end
      
          def student_id
              return numberhere
          end
      
          def setplayer1
              @player1 = 0
          end
      
          def setplayer2
              @player2 = 1
          end
      
          def clearmatrix
              @matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
          end
      
          def getmatrixvalue(n)
              @matrix[n]
          end
      
          def setmatrixvalue(i, v)
              @i = 1
              @v = "0"
              @matrix[i] = "0"            
          end
      
          def displaykey(matrix)
              @matrix = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
              @output.puts "Table key:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
          end
      
          def displaymatrix
              @matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
              @output.puts "Table status:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
          end
      
          def finish
              @output.puts "Finishing game..."
          end
      
          def displaymenu
              @output.puts "Menu: (1)Start | (2)New | (9)Exit\n"
          end
      
          def checkwinner
              if @matrix[0] = "0" && @matrix[1] = "0" && @matrix[2] = "0" then
                  winner = 1
              elsif @matrix[3] = "0" && @matrix[4] = "0" && @matrix[5] = "0" then
                  winner = 1
              elsif @matrix[6] = "0" && @matrix[7] = "0" && @matrix[8] = "0" then
                  winner = 1
              elsif @matrix[0] = "0" && @matrix[3] = "0" && @matrix[6] = "0" then
                  winner = 1
              elsif @matrix[1] = "0" && @matrix[4] = "0" && @matrix[7] = "0" then
                  winner = 1  
              elsif @matrix[2] = "0" && @matrix[5] = "0" && @matrix[8] = "0" then
                  winner = 1
              elsif @matrix[0] = "0" && @matrix[4] = "0" && @matrix[8] = "0" then
                  winner = 1
              elsif @matrix[2] = "0" && @matrix[4] = "0" && @matrix[6] = "0" then
                  winner = 1
              elsif @matrix[0] = "1" && @matrix[1] = "1" && @matrix[2] = "1" then
                  winner = 2
              elsif @matrix[3] = "1" && @matrix[4] = "1" && @matrix[5] = "1" then
                  winner = 2
              elsif @matrix[6] = "1" && @matrix[7] = "1" && @matrix[8] = "1" then
                  winner = 2
              elsif @matrix[0] = "1" && @matrix[3] = "1" && @matrix[6] = "1" then
                  winner = 2
              elsif @matrix[1] = "1" && @matrix[4] = "1" && @matrix[7] = "1" then
                  winner = 2  
              elsif @matrix[2] = "1" && @matrix[5] = "1" && @matrix[8] = "1" then
                  winner = 2
              elsif @matrix[0] = "1" && @matrix[4] = "1" && @matrix[8] = "1" then
                  winner = 2
              elsif @matrix[2] = "1" && @matrix[4] = "1" && @matrix[6] = "1" then
                  winner = 2  
              end
          end
      
      # Any code/methods aimed at passing the RSpect tests should be added above.
      
      end
      end
      
      module OXs_Game
      @input = STDIN
      @output = STDOUT
      @g = Game.new(@input, @output)
      @matrixkey = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
      @matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
      @playing = true
      @input = ""
      @option = 0
      @turn = 0
      
      #Any code added to output the activity messages to the command line window should be added below.
      
      def self.startup
          @g.start
          @g.displaykey(@matrixkey)
          @g.displaymatrix
          puts "Please select an option:"
          @g.displaymenu
          menu_input = gets.chomp
      end
      
      def menu
          if menu_input == 1 then
              player1turn
          elsif menu_input == 2 then
              @g.clearmatrix
              startup
          elsif menu_input == 9 then
              exit
          end
      end
      
      def player1turn
          @g.displaymatrix
          puts "Player 1 to move."
          player1input = gets.chomp
          if player1input == 1 then
              @matrix[0] == "0"
          elsif player1input == 2 then
              @matrix[1] == "0"
          elsif player1input == 3 then
              @matrix[2] == "0"
          elsif player1input == 4 then
              @matrix[3] == "0"
          elsif player1input == 5 then
              @matrix[4] == "0"
          elsif player1input == 6 then
              @matrix[5] == "0"
          elsif player1input == 7 then
              @matrix[6] == "0"   
          elsif player1input == 8 then
              @matrix[7] == "0"   
          elsif player1input == 9 then
              @matrix[8] == "0"
          else
              puts "Invalid input, please try again."
              player1turn
          end
      
      end 
      startup
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-17
        • 1970-01-01
        • 2015-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多