0

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?

1
  • 1
    Bad question. You have many variables/methods without explanation (nums, qs, question, answer). You also are not closing when with end. Commented May 15, 2011 at 20:10

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

3 Comments

You can also rewrite n = nums[rand(nums.length)] to be: n = nums.sample
You should be using to_s, not to_str.
Adding to @Phrogz 's comment, here is an article on to_s vs to_str

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.