0

任何人都可以帮助一个相对较新的红宝石人,看看为什么我得到这个没有方法错误?将不胜感激!

def comp_block
only_user_valued = @winning_propositions.map { |each_hash| each_hash.select { |key, value| value == @user_sign } }
count_of_each = only_user_valued.map { |count_the_items_in_hash| count_the_items_in_hash.count }
index_array = count_of_each.each_with_index.select { |num, index| num == 2 }.map { |index_spot| index_spot[1] }
  if index_array.empty? == true
    random_move
  else
    @nil_valued_values_array = []
    @nil_valued_array_true_false = []
    index_array.each do |element|
      @nil_valued_values_array += [@winning_propositions[element].select { |key, value| value == nil }]
      @nil_valued_array_true_false += [@nil_valued_values_array.empty?]
    end

nil_value = @nil_valued_values_array.delete({})
move = nil_value[0].keys[0]

    if @nil_valued_array_true_false == [false] || @nil_valued_array_true_false == [true, false] || @nil_valued_array_true_false == [false, true]
      @possible_places[move] = @comp_sign
        #changes the winning prop values in parallel
          list_of_matching_arrays=@winning_propositions.select { |key, value| key.to_s.match(move.to_s) }
            list_of_matching_arrays.each do |change_hash_value|
              change_hash_value[move] = @comp_sign
            end
      puts @comp_name + " made the move: #{move}"
      display_game_board
      puts "Here I am defending/BLOCKED!!!!!!"
    else @nil_valued_array_true_false == [true] || @nil_valued_array_true_false == [true, true]
     random_move
    end
  end
end
4

2 回答 2

0

Well, this isn't exactly a fix (since you've got a bunch of code, not sure which things are happening externally), but you should add some debugging statements.

Look here:

move = nil_value[0].keys[0]

If either nil_value or keys are nil, you're going to get that error obviously. I suggest you print their values and see which one is null:

puts "<<<<< NIL_VALUE: #{nil_value}"
puts "<<<<< NIL_VALUE[0]: #{nil_value[0]}"
puts "<<<<< NIL_VALUE[0].KEYS: #{nil_value[0].keys}"

so on and so forth.

于 2013-11-12T22:54:29.513 回答
0

好吧,显然不是nil_valueor nil_value[0].keysis nil。看两行代码:

nil_value = @nil_valued_values_array.delete({})
move = nil_value[0].keys[0]

最明显的原因是@nil_valued_values_array.delete({})没有找到要删除的空哈希,因此返回 nil,或者如果找到了,则返回一个空哈希。

于 2013-11-12T22:57:20.683 回答