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!
You probably have a newline in your input you haven’t stripped. When processing data from files, be sure to
chompany input values. Even better is to callstripwhich will remove leading and trailing spaces.Whenever trying to do diagnostics, it’s important to show “invisible” characters:
This would probably have exposed the issue sooner.
inspectcan 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.