main
 1class Cell
 2  attr_reader :x, :y
 3
 4  def initialize(alive: false, x: 1, y: 1)
 5    @alive = alive
 6    @x, @y = x, y
 7  end
 8
 9  def spawn(world)
10    if alive?
11      Cell.new(alive: (2...4).include?(living_neighbors_in(world).count), x: x, y: y)
12    else
13      Cell.new(alive: living_neighbors_in(world).count == 3, x: x, y: y)
14    end
15  end
16
17  def neighbor?(other_cell)
18    return true if x?(other_cell) && one_cell_away(other_cell.y - self.y)
19    return true if y?(other_cell) && one_cell_away(other_cell.x - self.x)
20    false
21  end
22
23  def alive?
24    @alive
25  end
26
27  private
28
29  def living_neighbors_in(world)
30    world.neighbors_for(self).find_all { |x| x.alive? }
31  end
32
33  def x?(other_cell)
34    other_cell.x == x
35  end
36
37  def y?(other_cell)
38    other_cell.y == y
39  end
40
41  def one_cell_away(difference)
42    difference.abs == 1
43  end
44end