打印

求助:如何才能使键盘输入与程序中的字符串相等

求助:如何才能使键盘输入与程序中的字符串相等

result =
if gets == "123" then true
else false
end
puts result

输入123后
以上代码输出false。为什么

如何才能使键盘输入的字符串和程序中的字符串相等
使得进行比较后得到true

这种写法明明可以的

result =
if "123" == "123" then true
else false
end
puts result

这段代码就可以输出true。

TOP



result = gets
if result == "123\n"
  puts true
else
  puts false
end


TOP

非常感谢,果然是"\n"的问题

TOP

另外又找到了另一个方法。
gets.chomp可以去掉结尾的回车。
"hello".chomp » "hello" 
"hello\n".chomp » "hello" 
"hello \n there".chomp » "hello \n there" 
"hello".chomp("llo") » "he"

TOP

2008-12-05 05:02 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html) @38.103.63.61