I am making conway game of life in ruby. This is my cell_spec.rb file. I'm getting failure/error in line 10 as:
expect(GameOfLife::Cell.new_dead_cell).to be_dead
I have a another file cell.rb where class cell is defined. How will implement custom predicate mather in this file?
require 'spec_helper'
describe GameOfLife::Cell do
def build_neighbours(live_count)
[GameOfLife::Cell.new_live_cell] * live_count +
[GameOfLife::Cell.new_dead_cell] * (8 - live_count)
end
context "factory" do
it "can be dead" do
expect(GameOfLife::Cell.new_dead_cell).to be_dead
end
it "can be alive" do
expect(GameOfLife::Cell.new_live_cell).to be_alive
end
end
context "live cell generation rules" do
let(:cell) { GameOfLife::Cell.new_live_cell }
[0, 1].each do |i|
it "dies when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_dead
end
end
[2, 3].each do |i|
it "lives when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_alive
end
end
(4..8).each do |i|
it "dead when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_dead
end
end
end
context "dead cell generation rules" do
let(:cell) { GameOfLife::Cell.new_dead_cell }
(0..2).each do |i|
it "dies when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_dead
end
end
[3].each do |i|
it "lives when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_alive
end
end
(4..8).each do |i|
it "dead when there are #{i} live neighbours" do
expect(cell.next_generation(build_neighbours(i))).to be_dead
end
end
end
end
this is my cell.rb file having cell class.. i want to know the inplementation of code for dead? and alive? methods. plz help me out
class GameOfLife::Cell
ALIVE = "alive"
DEAD = "dead"
# lost implementation
def self.new_dead_cell
return DEAD
end
def self.new_live_cell
return ALIVE
end
def dead?
end
def alive?
end
end
Here's one obvious way of doing this. You simply create a new instance of Cell and store its state (as :dead or :alive). dead? and alive? methods then simply check the state.
class Cell
ALIVE = :alive
DEAD = :dead
def self.new_dead_cell
new(DEAD)
end
def self.new_live_cell
new(ALIVE)
end
def initialize state
@state = state
end
attr_reader :state
def dead?
state == DEAD
end
def alive?
state == ALIVE
end
end