输入值的条件始终返回false(RUBY)

输入值的条件始终返回false(RUBY),ruby,Ruby,我想知道为什么即使我输入“y”,条件也总是错误的 puts "Would you like to continue [y/n]" confirm = gets puts confirm == "y" # why this is not true even I type "y" if confirm == "y" puts "Input is y" end 尝试使用confirm=gets.chompcausegets设置最后输入的内容\n > a = gets >

我想知道为什么即使我输入“y”,条件也总是错误的

puts "Would you like to continue [y/n]"
confirm = gets
puts confirm == "y" # why this is not true even I type "y"    

if confirm == "y"
    puts "Input is y"
end

尝试使用
confirm=gets.chomp
cause
gets
设置最后输入的内容
\n

> a = gets
> y
 => "y\n" 

请尝试此代码

puts "Would you like to continue [y/n]"
confirm = gets
puts "|#{confirm}|" # y\n
puts confirm.strip == "y" # gives you true
puts confirm == "y" # gives you false

if confirm == "y"
    puts "Input is y"
end

您可以使用
gets.strip
gets.chomp
gets

中删除任何不必要的字符。谢谢你,我会在9分钟后给它打分