Jake Burton
Jake Burton

Reputation: 19

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 false:

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?

Upvotes: 0

Views: 1254

Answers (1)

Mario Visic
Mario Visic

Reputation: 2663

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

Upvotes: 4

Related Questions