title: How to Use Hashes to Store methods in Ruby?
body: <p>I was wondering if anyone could explain to me why I can't use my <code>Hash($player_x_command_list)</code> to access my <code>player_x_move(position)</code> method? What's the correct way of doing this?</p>
  
  <p>What I am trying to do is, create a Hash that lets the user enter input and it reflecting back to my methods in my class.</p>
  
  <p>I have tried various ways of altering so that the code could work correctly but nothing works.</p>
  
  <p>I'm getting the error:</p>
  
  <blockquote>
    <p>undefined method `player_x_move' for Game:Class (NoMethodError)</p>
  </blockquote>
  
  <p>Does that mean hashes can't store methods?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#Tic Tac Toe Game
  #The format is the below: where index 0 represents top left and index 8 represents bottom right
  #goes 0,1,2
  #     3,4,5
  #     6,7,8
  #"e" is for empty. "x" is for Player X moves and "o" is for Player O moves
  
  
  
  class Game
    @@player_x_win_count = 0
    @@player_o_win_count = 0
  
    def initialize
      @board = Array.new(9, "e")
      @move_number = 0
    end
  
    def get_names
    puts "Hi Welcome to my Tic Tac Toe Game. The board looks like this:
  
    |TL|TM|TR|
    |ML|MM|MR|
    |BL|BM|BR|
  
    Each position of the Tic Tac Toe board is represented by two letters. To \"X\" or \"O\" a position, just input the two letters in CAPS like \"MM\"
  
  
    The command list is as follows:
    TL = top left
    TM = top mid
    TR = top right
    ML = mid left
    MM = mid mid
    MR = mid right
    BL = bottom left
    BM = bottom mid
    BR = bottom right
    board = to view the board
    new game = to clean the board and create a new game (note that this command should only be used when you don't want to continue on the current game. The game automatically creates a new game if a winner, loser, or draw is declared)
     "
      puts "Please Enter PlayerX's name. He/she will be using X's to mark the board."
      @player_x = gets.chomp
  
      puts "Please Enter PlayerO's name. He/she will be using O's to mark the board."
      @player_o = gets.chomp
  
      self.new_round
    end
  
  $player_x_command_list = {"TL" =&gt; self.player_x_move(0), "TM" =&gt; self.player_x_move(1), "TR" =&gt; self.player_x_move(2), "ML" =&gt; self.player_x_move(3), "MM" =&gt; self.player_x_move(4), 
      "MR" =&gt; self.player_x_move(5), "BL" =&gt; self.player_x_move(6), "BM" =&gt; self.player_x_move(7), "BR" =&gt; self.player_x_move(8), "board" =&gt; self.board, "new game" =&gt; self.clean_board, 
      "win count" =&gt; self.win_count}
  
  $player_o_command_list = {"TL" =&gt; self.player_o_move(0), "TM" =&gt; self.player_o_move(1), "TR" =&gt; self.player_o_move(2), "ML" =&gt; self.player_o_move(3), "MM" =&gt; self.player_o_move(4), 
      "MR" =&gt; self.player_o_move(5), "BL" =&gt; self.player_o_move(6), "BM" =&gt; self.player_o_move(7), "BR" =&gt; self.player_o_move(8), "board" =&gt; self.board, "new game" =&gt; self.clean_board, 
      "win count" =&gt; self.win_count}
  
  
    def enter_command_player_x
      puts "Please input your command, #{@player_x} aka PlayerX"
      command = gets.chomp
      $player_x_command_list[command]
    end
  
    def enter_command_player_o
      puts "Please input your command, #{@player_o} aka PlayerY. Type \"help\" to see a full list of commands"
      command = gets.chomp
      $player_o_command_list[command]
    end
  
  
    def new_round
      puts "So who wants to go first this round"
      went_first_this_round = gets.chomp
      if went_first_this_round == @player_x
        self.enter_command_player_x
      elsif went_first_this_round == @player_o
        self.enter_command_player_o
      else
        puts "Not a valid name. Please enter one of the player's names"
      end
    end
  
    def board
      print "|#{@board[0]}|#{@board[1]}|#{@board[2]}|\n|#{@board[3]}|#{@board[4]}|#{@board[5]}|\n|#{@board[6]}|#{@board[7]}|#{@board[8]}|"
    end
  
    def player_x_move(position)
      if @board[position] == "x" || @board[position] == "o"  
        return "That move was invalid as someone has already moved there. Please enter a valid move"
      end
      @board[position] = "x"
      @move_number += 1
      puts "That was move number #{@move_number} and the current board looks like: " 
      self.board
      self.check_game
      puts "Now it is #{player_o}'s turn. #{player_o} please input your next command."
      self.enter_command_player_o
    end
  
    def player_o_move(position)
      if @board[position] == "x" || @board[position] == "o"     
        return "That move was invalid as someone has already moved there. Please enter a valid move"
      end
      @board[position] = "o"
      @move_number += 1
      puts "That was move number #{@move_number} and the current board looks like: "
      self.board
      self.check_game
      puts "Now it is #{player_x}'s turn. #{player_x} please input your next command"
      self.enter_command_player_x
    end
  
    def check_game
      triple_x = "xxx"
      triple_o = "ooo"
      if @move_number == 9
        @move_number = 0
        self.clean_board
        return "The board is completely filled up. Looks like this is a draw. This is Game Over. Make a new game by setting any variable = to new.Game and using that variable to play"
      elsif @board[0] + @board[1] + @board[2] == triple_x
        @@player_x_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player X Wins"
      elsif @board[3] + @board[4] + @board[5] == triple_x
        @@player_x_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player X Wins"
      elsif @board[6] + @board[7] + @board[8] == triple_x
        @@player_x_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player X Wins"
      elsif @board[0] + @board[3] + @board[6] == triple_x
        @@player_x_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player X Wins"
      elsif @board[1] + @board[4] + @board[7] == triple_x
        @@player_x_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player X Wins"
      elsif @board[2] + @board[5] + @board[8] == triple_x
        @@player_x_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player X Wins"
      elsif @board[0] + @board[4] + @board[8] == triple_x
        @@player_x_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player X Wins"
      elsif @board[2] + @board[4] + @board[6] == triple_x
        @@player_x_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player X Wins"
      #now check if Player O Wins
      elsif @board[0] + @board[1] + @board[2] == triple_o
        @@player_y_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player O Wins"
      elsif @board[3] + @board[4] + @board[5] == triple_o
        @@player_y_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player O Wins"   
      elsif @board[6] + @board[7] + @board[8] == triple_o
        @@player_y_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player O Wins"   
      elsif @board[0] + @board[3] + @board[6] == triple_o
        @@player_y_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player O Wins"    
      elsif @board[1] + @board[4] + @board[7] == triple_o
        @@player_y_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player O Wins"   
      elsif @board[2] + @board[5] + @board[8] == triple_o
        @@player_y_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player O Wins"  
      elsif @board[0] + @board[4] + @board[8] == triple_o
        @@player_y_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player O Wins" 
      elsif @board[2] + @board[4] + @board[6] == triple_o
        @@player_y_win_count += 1
        @move_number = 0
        self.clean_board
        return "Player O Wins"   
      else
        return "no one has WON YET! Continue your GAME!!"
      end
    end
  
    def clean_board
      @board = Array.new(9, "e")
    end
  
    def win_count
      puts "So far Player X has won #{@@player_x_win_count} times and Player O has won #{@@player_o_win_count} times."
    end
  
  end
  
  a = Game.new
  a.get_names
  </code></pre>