开发者

Why does comparing strings in Ruby always return false?

开发者 https://www.devze.com 2023-03-06 05:29 出处:网络
I use the following to check whether the answer to a posed question is true or fal开发者_开发百科se:

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

精彩评论

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