私は、次のコードでは型エラーを取得しているようだ:`[] ':整数に文字列のいない暗黙の変換(TypeError例外)
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のプログラマーではありませんが、文字列をIntegerとして使用しようとしていると不満を持ち、あなたが知らないうちにそうしたくないという不満があります。 to_iを見てください:https://apidock.com/ruby/String/to_i – Qrchack
例外に関する情報を提供する際には、それを発生させたコード行を含めてください。 –