【问题标题】:`[]': no implicit conversion of String into Integer (TypeError)`[]': 没有将 String 隐式转换为 Integer (TypeError)
【发布时间】:2017-05-03 19:51:56
【问题描述】:

我似乎在以下代码中遇到了类型错误:

    def can_cast(hand, *spell_cost)
      colored_mana_hand = Array.new
      colored_mana_cost_aggregate = Array.new
      colored_mana_spent = Array.new
      colorless_mana_hand = 0
      colorless_mana_cost_aggregate = 0

      hand_array = hand.split("").sort
      total_cost = spell_cost.join.split("").sort   

      hand_array.each do |i|
      if hand_array[i].to_i != 0                       
        colorless_mana_hand += hand_array[i].to_i
      else
        colored_mana_hand << hand_array[i]
      end
    end

    total_cost.each do |i|                 
      if total_cost[i].to_i != 0                  
        colorless_mana_cost_aggregate += total_cost[i].to_i
      else
        colored_mana_cost_aggregate << total_cost[i]
      end
    end

colored_mana_cost_aggregate.each do |i|                                                
  if colored_mana_hand.include?(colored_mana_cost_aggregate[i])
     colored_mana_spent << colored_mana_cost_aggregate[i]           
colored_mana_hand.rotate(colored_mana_hand.index(colored_mana_cost_aggregate[i])).shift
  end
end

   colored_mana_spent == colored_mana_cost_aggregate && (colored_mana_hand.length + colorless_mana_hand) >= colorless_mana_cost_aggregate 
   end

看起来像这样

   `[]': no implicit conversion of String into Integer (TypeError)

有人可以帮我吗?

我认为我将数组用作整数,但我看不出在哪里可能。

【问题讨论】:

  • 不是 Ruby 程序员,但它抱怨您试图将 String 用作 Integer 并且它不想在您不知情的情况下这样做。看看to_i:apidock.com/ruby/String/to_i
  • 在提供异常信息时,请包括引发异常的代码行。

标签: ruby array-algorithms


【解决方案1】:

正如 Brian 所说,您误解了 each 如何与块参数一起使用

代替

hand_array.each do |i|
  if hand_array[i].to_i != 0                       
    colorless_mana_hand += hand_array[i].to_i
  else
    colored_mana_hand << hand_array[i]
  end
end

你只是引用元素

hand_array.each do |hand|
  if hand.to_i != 0                       
    colorless_mana_hand += hand.to_i
  else
    colored_mana_hand << hand
  end
end

如果你真的想要数组的索引,可以使用each_with_index

hand_array.each_with_index do |_hand, i|
  if hand_array[i].to_i != 0                       
    colorless_mana_hand += hand_array[i].to_i
  else
    colored_mana_hand << hand_array[i]
  end
end

或者只使用一系列索引值

(0...hand_array.count).each do |i|
  if hand_array[i].to_i != 0                       
    colorless_mana_hand += hand_array[i].to_i
  else
    colored_mana_hand << hand_array[i]
  end
end

至于错误的确切含义...如果hand_array包含诸如“7”之类的字符串,那么i包含“7”并且您正在尝试访问hand_array[“7”]...换句话说尝试使用字符串而不是整数访问数组元素。并且 Ruby 不会自动(隐式)将字符串转换为数组索引的整数。

【讨论】:

    【解决方案2】:

    这条线似乎很可疑:

    if colored_mana_hand.include?(colored_mana_cost_aggregate[i])
    

    在这种情况下,i 是colored_mana_cost_aggregate 的一个元素(来自前一行的each 迭代器),您正试图将其用作数组索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      相关资源
      最近更新 更多