开发者

Why does comparing strings in Ruby always return false?

I use the following to check whether the answer to a posed question is true or fal开发者_开发百科se:

when "new" 
  n = nums[rand(nums.length)]
  puts "Question:"
  puts qs[n].question
  torf = gets.downcase.to_str.eql? qs[n].answer.downcase.to_str
  puts torf

But the result torf is always false even if the right answer is put. What am I missing?


gets will return the entered string plus a newline charcter, so you'll need to remove it. As it is a string you don't need to run to_str on the result either.

 torf = gets.downcase.chomp.eql? qs[n].answer.downcase.to_s
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜