5
I’m studying a book of programming logic and wanted to know what it means <<
see what the book is applying to:
This balcony to maintain a help variable with the actual size used an array is very important and extremely used in several situations. We can remove our total_de_chutes variable and use the size :
limite_de_tentativas = 5 chutes = [] for tentativa in 1..limite_de_tentativas chute = pede_um_numero chutes, tentativa, limite_de_tentativas chutes[chutes.size] = chute if verifica_se_acertou numero_secreto, chute break end end
But if Ruby has something to help us with the current size of a array, does it no longer have something that helps us to put a value at the end of it? Yes, we have seen the symbol << :
chutes = [100, 300, 500] chutes << 600 puts chutes.size # imprime 4 puts chutes[3] # imprime 600
Therefore, our final code becomes even simpler:
da_boas_vindas numero_secreto = sorteia_numero_secreto limite_de_tentativas = 5 chutes = [] for tentativa in 1..limite_de_tentativas chute = pede_um_numero chutes, tentativa, limite_de_tentativas chutes << chute if verifica_se_acertou numero_secreto, chute break end end
Welcome Raphael, while an answer does not arrive, Choose this question What does << Mean in Ruby? in English
– Luiz Augusto