Class: Lifelike::Grid

Inherits:
Object
  • Object
show all
Defined in:
lib/lifelike/grid.rb

Defined Under Namespace

Classes: GridSerializer

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows) ⇒ Grid

Returns a new instance of Grid.



3
4
5
# File 'lib/lifelike/grid.rb', line 3

def initialize(rows)
  @rows = rows
end

Class Method Details

.from_s(string) ⇒ Object



21
22
23
24
25
# File 'lib/lifelike/grid.rb', line 21

def self.from_s(string)
  GridSerializer.load(string) do |char|
    yield(char)
  end
end

Instance Method Details

#map_with_neighborsObject



7
8
9
10
11
12
13
14
15
# File 'lib/lifelike/grid.rb', line 7

def map_with_neighbors
  self.class.new(
    @rows.map.with_index do |row, row_index|
      row.map.with_index do |cell, col_index|
        yield(cell, neighbors(row_index, col_index))
      end
    end
  )
end

#to_aObject



17
18
19
# File 'lib/lifelike/grid.rb', line 17

def to_a
  @rows
end

#to_sObject



27
28
29
30
31
# File 'lib/lifelike/grid.rb', line 27

def to_s
  GridSerializer.dump(self) do |cell|
    yield(cell)
  end
end