3

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
Manish Singh
  • 151
  • 1
  • 10
  • Please add the complete error message to your question. – Patrick Oscity May 27 '15 at 07:04
  • 1) GameOfLife::Cell factory can be dead Failure/Error: expect(GameOfLife::Cell.new_dead_cell).to be_dead expected dead to respond to `dead?` # ./spec/game_of_life/cell_spec.rb:10:in `block (3 levels) in ' 2) GameOfLife::Cell factory can be alive Failure/Error: expect(GameOfLife::Cell.new_live_cell).to be_alive expected alive to respond to `alive?` # ./spec/game_of_life/cell_spec.rb:14:in `block (3 levels) in ' – Manish Singh May 27 '15 at 08:05
  • It pretty much tells you what's wrong with your code. In order to make the `be_dead` matcher work, you need to implement the `dead?` method in the `GameOfLife::Cell` class. – Patrick Oscity May 27 '15 at 09:05
  • i have implemented dead? method..but this the error is "expected dead to respond to dead?".. how will this problem be solved def dead? dead end – Manish Singh May 27 '15 at 09:21
  • What do you return from the `new_dead_cell` method? It should return the instance, so the last line should be `self`, unless the last statement in the method already returns the instance. – Patrick Oscity May 27 '15 at 09:25
  • Please edit your post if you want to paste longer text and useful info for other readers. If you want to get good answers, you need to make it as easy as possible for readers to quickly get all the necessary information. – Patrick Oscity May 27 '15 at 09:26
  • this is code for cell class.. what i need to write in dead? and alive? methods. i am stuck here 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 – Manish Singh May 27 '15 at 09:33
  • Please add your cell class to the original question, you can edit it. – Patrick Oscity May 27 '15 at 10:00
  • i have added cell.rb file. i want to know about implmentation of dead? and alive? methods – Manish Singh May 27 '15 at 16:58
  • What do you want to know about them? – Dave Newton May 27 '15 at 17:08
  • the code inside dead? and alive?. i am not able to get what will come inside these methods – Manish Singh May 27 '15 at 17:24

1 Answers1

0

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
sinjed
  • 906
  • 7
  • 10