Commit eaaaf55

mo khan <mo@mokhan.ca>
2014-06-23 03:34:23
extract method to find living neighbors.
1 parent 34ee148
Changed files (1)
lib
lib/gol/cell.rb
@@ -7,11 +7,10 @@ class Cell
   end
 
   def spawn(world)
-    living_neighbors = world.neighbors_for(self).find_all { |x| x.alive? }
     if alive?
-      Cell.new(alive: (2...4).include?(living_neighbors.count), x: x, y: y)
+      Cell.new(alive: (2...4).include?(living_neighbors_in(world).count), x: x, y: y)
     else
-      Cell.new(alive: living_neighbors.count == 3, x: x, y: y)
+      Cell.new(alive: living_neighbors_in(world).count == 3, x: x, y: y)
     end
   end
 
@@ -27,6 +26,10 @@ class Cell
 
   private
 
+  def living_neighbors_in(world)
+    world.neighbors_for(self).find_all { |x| x.alive? }
+  end
+
   def x?(other_cell)
     other_cell.x == x
   end