2
我不知道为什么,即使我键入“Y”的条件总是假条件输入值总是返回false(RUBY)
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
我不知道为什么,即使我键入“Y”的条件总是假条件输入值总是返回false(RUBY)
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
原因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分钟后回答它 – Beginner