我想知道为什么即使输入“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
答案 0 :(得分:1)
尝试使用gets
因为\n
设置您最后输入> a = gets
> y
=> "y\n"
的内容
class
答案 1 :(得分:1)
请尝试使用此代码。
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
中删除任何不必要的字符