2017-05-03 6 views
0

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.

+0

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

+2

Lors de la fourniture d'informations sur les exceptions s'il vous plaît inclure la ligne de code qui l'a soulevé. –

Répondre

1

Cette ligne semble suspect:

if colored_mana_hand.include?(colored_mana_cost_aggregate[i]) 

Dans ce contexte, i est un élément de colored_mana_cost_aggregate (du each iterator sur la ligne avant) et que vous essayez de l'utiliser comme un index de tableau.

+0

Oh c'est clair merci l'homme. –

3

Comme le dit Brian, vous interprétant mal comment les each fonctionne avec le paramètre de bloc

Au lieu de

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 

Vous faites référence à tout l'élément

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

Si vous voulez vraiment la index du tableau, vous pouvez utiliser 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 

ou tout simplement utiliser une gamme de valeurs d'indice

(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 

Quant à exactement ce que signifie l'erreur ... si le hand_array contient des chaînes telles que « 7 » alors i contient « 7 » et vous êtes essayer d'accéder à hand_array ["7"] ... en d'autres termes essayer d'accéder à un élément de tableau en utilisant une chaîne au lieu d'un entier. Et Ruby ne convertira pas automatiquement (implicitement) une chaîne en un entier pour un index de tableau.