Proc causing a random TypeError
Posted
by
go____yourself
on Stack Overflow
See other posts from Stack Overflow
or by go____yourself
Published on 2014-06-03T02:30:58Z
Indexed on
2014/06/03
3:26 UTC
Read the original article
Hit count: 150
ruby
I'm refactoring some code and this proc
is causing an error randomly and I don't know why or how to debug it... Any ideas?
New code with proc
defense_moves, offense_moves = [], []
determine_move = ->move,side,i { side << move.count(move[i]) }
defense.size.times { |i| determine_move.(defense, defense_moves, i) }
offense.size.times { |i| determine_move.(offense, offense_moves, i) }
dm = defense[defense_moves.index(defense_moves.max)].nil? ? [0] : defense[defense_moves.index(defense_moves.max)]
om = offense[offense_moves.index(offense_moves.max)].nil? ? [0] : offense[offense_moves.index(offense_moves.max)]
Original code:
d = 0
defense_moves = []
loop do
defense_moves << defense.count(defense[d])
break if defense.count(defense[d]).zero?
d += 1
end
o = 0
offense_moves = []
loop do
offense_moves << offense.count(offense[o])
break if offense.count(offense[o]).zero?
o += 1
end
dm = defense[defense_moves.index(defense_moves.max)].nil? ? [0] : defense[defense_moves.index(defense_moves.max)]
om = offense[offense_moves.index(offense_moves.max)].nil? ? [0] : offense[offense_moves.index(offense_moves.max)]
TypeError
ttt2.rb:95:in `[]': no implicit conversion from nil to integer (TypeError)
from ttt2.rb:95:in `computer_make_move'
from ttt2.rb:133:in `draw_board'
from ttt2.rb:24:in `place'
from ttt2.rb:209:in `block in start_new_game'
from ttt2.rb:188:in `loop'
from ttt2.rb:188:in `start_new_game'
from ttt2.rb:199:in `block in start_new_game'
from ttt2.rb:188:in `loop'
from ttt2.rb:188:in `start_new_game'
from ttt2.rb:199:in `block in start_new_game'
from ttt2.rb:188:in `loop'
from ttt2.rb:188:in `start_new_game'
from ttt2.rb:199:in `block in start_new_game'
from ttt2.rb:188:in `loop'
from ttt2.rb:188:in `start_new_game'
from ttt2.rb:199:in `block in start_new_game'
from ttt2.rb:188:in `loop'
from ttt2.rb:188:in `start_new_game'
from ttt2.rb:234:in `<main>'
© Stack Overflow or respective owner