diff options
| author | Teddy Wing | 2015-04-14 17:27:31 -0400 | 
|---|---|---|
| committer | Teddy Wing | 2015-04-14 17:27:31 -0400 | 
| commit | eba2547a194b801596b43f6ba37b7d1b867a321d (patch) | |
| tree | e0a845af0cac64fa3e17752cf14a5cbeb988825e | |
| parent | 1863abc09ec0af8a0026d4c170bfd50b11032b76 (diff) | |
| download | tic-tac-toe-eba2547a194b801596b43f6ba37b7d1b867a321d.tar.bz2 | |
Board: Method to unintelligently convert string coordinates to array
Take string coordinates in the form ":integer:,:integer:" and convert
them to [integer, integer]. We'll be using these converted coordinate
values to place a piece on the board.
| -rw-r--r-- | board.rb | 13 | ||||
| -rw-r--r-- | spec/board_spec.rb | 13 | 
2 files changed, 26 insertions, 0 deletions
| @@ -10,4 +10,17 @@ class Board    def render      "...\n" * 3    end +   +  # Raises an ArgumentError if integer conversion fails +  def transform_coordinates(str) +    coordinates = str.split(',') +     +    begin +      coordinates[0] = Integer(coordinates[0]) +      coordinates[1] = Integer(coordinates[1]) +       +      coordinates if coordinates.length > 1 +    rescue +    end +  end  end diff --git a/spec/board_spec.rb b/spec/board_spec.rb index 86b4276..8d2b8da 100644 --- a/spec/board_spec.rb +++ b/spec/board_spec.rb @@ -22,4 +22,17 @@ EOF        ]      end    end +   +  describe '#transform_coordinates' do +    it 'converts string coordinates to an array' do +      @board.transform_coordinates('0,4').must_equal [0, 4] +    end +     +    it "returns nil if coordinates don't match the format" do +      @board.transform_coordinates('4').must_be_nil +      @board.transform_coordinates('4 2').must_be_nil +      @board.transform_coordinates('booyakacha').must_be_nil +      @board.transform_coordinates('booya,kacha').must_be_nil +    end +  end  end | 
