条件输入值总是返回false(RUBY)

时间:2016-09-23 06:14:51

标签: 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

2 个答案:

答案 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.stripgets.chompgets中删除任何不必要的字符