Class: GameOfLife::Inputters::SimpleStringInputter

Inherits:
Object
  • Object
show all
Defined in:
lib/game_of_life/inputters/simple_string_inputter.rb

Overview

parses an input string for the board data

Instance Method Summary collapse

Instance Method Details

#convert_cryptic_symbol_to_state(symbol) ⇒ Object (private)



33
34
35
36
37
38
39
40
41
42
# File 'lib/game_of_life/inputters/simple_string_inputter.rb', line 33

def convert_cryptic_symbol_to_state(symbol)
  case symbol
  when 'X'
    :live
  when '-'
    :dead
  else
    raise ArgumentError, "Don't know how to convert #{symbol} into a Cell's state"
  end
end

#parse(raw_input) ⇒ 2D Array<Symbol>

Parses board’s data from a string.

Examples:

Will parse this input string

X - X
- - -
- X -

into this output structure:

[
  [:live, :dead, :live],
  [:dead, :dead, :dead],
  [:dead, :live, :dead]
]

Parameters:

  • raw_input (String)

    the given string

Returns:

  • (2D Array<Symbol>)

    formatted board data

Raises:

  • (ArgumentError)

    if anything other then an ‘X’ or ‘-’ as the state



23
24
25
26
27
28
29
30
# File 'lib/game_of_life/inputters/simple_string_inputter.rb', line 23

def parse(raw_input)
  rows = raw_input.split("\n")
  rows.map! { |row| row.split(' ') }
  rows.map! do |row|
    row.map! { |symbol| convert_cryptic_symbol_to_state(symbol) }
  end
  rows
end