Where’s my mistake in this Ruby code?

Asked

Viewed 147 times

0

I’m having trouble in methods with passing parameter, the code runs, but it moves when I pass var within one method and receives as parameter in another. I’m new to Ruby.

def player_welcome
    puts "Seja bem vindo ao Jogo de Adivinhação !!"
    puts "Criado por Thiago De Bonis Carvalho Saad Saud"
end

def generate_number_raffled
    number_raffled = 100
    number_raffled.to_i
end

def player_choice_name
    puts "Qual seu nome jogador?"
    player_name = gets
    player_name.to_s
end

def player_choice_attempts(player_name)
    puts "Quantas tentativas gostaria de ter " + player_name + "?" 
    number_attempts = gets
    number_attempts.to_i
end

def play_the_game(player_name,number_attempts)
    puts " " + player_name + "você tem " + number_attempts + "."
    for player_attempts in 1..number_attempts
        puts "Adivinhe um número entre 0 e 200..."
        player_kick = gets
        if check_number_raffled(player_kick,number_raffled)
            break
        end
    end
    player_kick.to_i
end

def check_number_raffled(player_kick,number_raffled)
    if player_kick == number_raffled
        puts "Você Acertou !!"
        return true
    end
    if player_kick > number_raffled
        puts "Você errou!!"
        puts "Você digitou um número maior que o do Sorteado, tente novamente.."
    else
        puts "Você errou!!"
        puts "Você digitou um número menor que o do Sorteado, tente novamente.."
        end
end

player_welcome
player_choice_name
player_choice_attempts(player_name)
play_the_game(player_name,number_attempts)

2 answers

1

  • I still haven’t learned this part of using @, only the #{nameVar} inside some String, would have another more practical way for those who are beginner, pass these variable parameters of functions for other functions? I added the following line of code, but it keeps repeating the code now, as if inside a While or calling the function all the time. '-'

  • Image: https://scontent.fsdu5-1.fna.fbcdn.net/v/t1.0-9/14713772_206737486415009_961632780851287076_n.jpg?oh=e484d87d21d415e4ddae82a9b1505bd2&oe=58A030DB

1

As Gleyson has already said, player_name is a local variable of the method player_choice_name.

His solution is valid, but a better solution than creating instance variables is to take the return of the method, method that you have already prepared to return the name of the player with the line player_name.to_s, and assign this return to another variable, which will be passed as parameter to the following methods:

def player_choice_name
  (...)
  player_name.to_s # Esse valor será retornado
end

(...)

player_welcome
player_name = player_choice_name
number_attempts = player_choice_attempts(player_name)
play_the_game(player_name, number_attempts)

Note that the variable number_attemps is also local and the same error would occur if the code arrived in the method call player_choice_attemps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.