Battlefield - undefined method 'write'

a bit weird. I finished my code, all specs passed, then dove in and started working on getting the game to run correctly. Finally got the basic version where I want it, went to double check the specs before I submitted, and I get this…

1) BattleshipGame#play_turn gets a move from the player and makes an attack at that position
     Failure/Error: game.play_turn
       undefined method `write' for #<Board:0x007f99858df290 @grid=[[:s, :s], [nil, nil]]>
     # ./lib/battleship.rb:21:in `display'
     # ./lib/battleship.rb:21:in `play_turn'
     # ./spec/battleship_spec.rb:51:in `block (3 levels) in <top (required)>'

no where in my code do I even have the word “write”

What’s on line 21 in battleship.rb? Post that and a few surrounding lines.

Fixed it. I wanted my my HumanPlayer to have a method that called a method on the instance of the board. The issue arose with how I was getting the board instance “into” the HumanPlayer so that the board method was called. Ended up setting up get_move to have an argument for the board, and then weaseled that arg input through to the method that I needed it to access (if that makes sense). Here is what I did for those that are interested

Battleship class vvv

def play_turn
    pos = player.get_play(board)
    puts "You have #{self.count} ships remaining!"

HumanPlayer class vvvv

def get_play(board)
    while true
      puts "Where would you like to attack Admiral #{name}? Ex: 1,2"
      pos = gets.chomp.split(",").map {|el| el.to_i}
      break if ok_move?(pos, board)

  def valid_entry?(pos)
    ##assumes a 10x10 board
    if pos.length == 2
       return true if pos.all? {|num| (0..9).include?(num)}

  def ok_move?(pos, board)
    if valid_entry?(pos) == false
      puts "That is not a valid entry, try again!"
      return false
    if board.valid_shot?(pos) == false
      puts "You've already gone there, pick again!"
      return false

Glad you figured it out!

I’m working on battleship now too, and I found out why it gives such a weird error message. display is a built-in ruby method. So if you call display on something (like an rspec double), and you haven’t given it your own display method, instead of saying
undefined method 'display'
it will say
undefined method 'write'
because the built in ruby display calls write.