Question

Looks like my use of the proc maybe a bit off. I'm working on a tic-tac-toe game and using cucumber to test it's behavior. I've outlined the scenario that i want to fulfill and the step file that I'm using.

The Scenario,

Scenario: Making Bad Moves
    Given I have a started Tic-Tac-Toe game
        And it is my turn
        And I am playing X
    When I enter a position "A1" on the board
        And "A1" is taken
    Then computer should ask me for another position "B2"
        And it is now the computer's turn 

The step files say...

Given /^I have a started Tic\-Tac\-Toe game$/ do #
  @game = TicTacToe.new(:player)
  @game.player = "player" #
end

Given /^it is my turn$/ do #
  @game.current_player.should eq "player"
end

Given /^I am playing X$/ do
  @game = TicTacToe.new(:computer, :X)
  @game.player_symbol.should eq :X
end


When /^"(.*?)" is taken$/ do |arg1|
  @game.board[arg1.to_sy m] = :O **- # this is causing me to get a "undefined

method `[]=' for #

Given /^I am playing X$/ do
  @game = TicTacToe.new(:computer, :X)
  @game.player_symbol.should eq :X
end

My code that is attempting to satisfy the feature is:

def board

    Proc.new do |get_player_move = :B2|
      board_value = get_player_move
      @board_locations[board_value]
    end

I get this error: NoMethodError: undefined method[]=' for #`

Am i using the proc properly?

Was it helpful?

Solution

Your problem is that [] and []= are in fact different methods. When you type:

@game.board[arg1.to_sym] = :O

ruby reads it as:

@game.board.[]=(arg1.to_sym, :o)

and what you want is

@game.board.[](arg1.to_sym) = :O

To make sure ruby knows what you want do:

(@game.board[arg1.to_sym]) = :O

NOTE:

To be honest I am not sure why you are using Proc here at all, why not simple:

def board
  @board_locations
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top