How do I collect a String and use it in an if? [closed]

3
puts "Tem dinheiro? s/n"
x = gets.chomp

if x = s
  puts "Vou viajar"
else
  puts "Não vai ser dessa vez"
end
    
asked by anonymous 29.11.2016 / 17:14

1 answer

1

First, you have to organize the code, not just play everything anyway.

Second, to compare a variable with a text is only possible if this text is expressed in its literal form, that is, it must be enclosed in quotation marks. Like a quote in a text.

Third, in%% is assigning a value to the variable with if , to compare and get a true or false the correct operator is = .

So:

puts "Tem dinheiro? s/n"
x = gets.chomp
if x == "s"
    puts "Vou viajar"
else
    puts "Não vai ser dessa vez"
end

See running on ideone .

I suggest taking a course on programming before attempting to write code at random.

    
29.11.2016 / 18:05