Proc导致随机TypeError

时间:2014-06-03 02:30:59

标签: ruby

我正在重构一些代码,这proc会导致错误随机发生,我不知道为什么或如何调试它......任何想法?

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)]

原始代码:

  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)]

类型错误

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>'

3 个答案:

答案 0 :(得分:1)

所以,在代码之前:

defense_moves, offense_moves = [], []
determine_move = -> move, side, i { side << move.count(move[i]) }
(defense.size + 1).times { |i| determine_move.(defense, defense_moves, i) }
(offense.size + 1).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)]

(defense/offence.size + 1),导致在添加到数组后中断。

我不确定它是否正确,但我猜它的效果与那个循环相同。

答案 1 :(得分:0)

loop do 
  defense_moves << defense.count(defense[d])
  ...
end

保证至少为defense_moves

分配一个元素
determine_move = ->move,side,i { side << move.count(move[i]) }
defense.size.times { |i| determine_move.(defense, defense_moves, i) }

没有提供此类保证。因此,defense_moves.maxdefense_moves.index(defense_moves.max)都是零,这会产生您的错误。

答案 2 :(得分:-1)

您的错误消息表示您已将nil传递给[]方法。我想它就在这一行

defense[defense_moves.index(defense_moves.max)]

或其他类似的。

如果defense_moves是一个空数组[].max #=> nil,那么如果你通过defense[nil],则会收到该错误。