Je semblent obtenir une erreur de type dans le code suivant:`[] ': pas de conversion implicite de chaîne en entier (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
Il ressemble à ce
`[]': no implicit conversion of String into Integer (TypeError)
pourrait Quelqu'un m'aider?
Je pense que j'utilise un tableau sous la forme d'un entier, mais je ne vois pas où cela pourrait être possible.
Ce n'est pas un programmeur Ruby, mais il se plaint que vous essayez d'utiliser une chaîne comme un nombre entier et le fait qu'il ne veut pas le faire à votre insu. Jetez un oeil à to_i: https://apidock.com/ruby/String/to_i – Qrchack
Lors de la fourniture d'informations sur les exceptions s'il vous plaît inclure la ligne de code qui l'a soulevé. –