0

The following program takes two inputs (comma separated) from the user:

puts "Enter the code"
input_codes = gets.split(',')
puts "your given code is: "

code_1 = input_codes[0]
code_2 = input_codes[1]

puts code_1=='GEO'
puts code_2=='TYP'

Output is the following:

Enter the code
> GEO,TYP

true
false

It should print true for both of the cases, right? Why is it printing false for the last case? What am I missing?

EDIT: Yeah, there was an extra newline character that I was missing in the beginning. Fixed the issue with the help of tadman. ANd yeah, the inspect thing was pretty cool and useful since then!

2 Answers 2

2

You probably have a newline in your input you haven't stripped. When processing data from files, be sure to chomp any input values. Even better is to call strip which will remove leading and trailing spaces.

gets.chomp.split(',')

Whenever trying to do diagnostics, it's important to show "invisible" characters:

puts code_2.inspect
# "TYP\n"

This would probably have exposed the issue sooner. inspect can be misleading, though, on custom classes that have their own customized emitter. It can't always be trusted, but it's usually a good place to start.

Sign up to request clarification or add additional context in comments.

Comments

2

You have a newline at the end of the string, just strip it:

input_codes = gets.strip.split(',')

Comments

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.