"-" == "-" returns false.. Why?

199 views Asked by At

enter image description here

Anybody know whats going on here? why is "-" not found? Try it in IRB.

if you do

string = "(( :H – :2B – :3B – :HR )+( 2 * :2B )+( 3 * :3B )+( 4 * :HR ))/ :AB "

string.split(" ")[2] == "-" 

it returns false as well.

3

There are 3 answers

1
August On BEST ANSWER

The character you get from string.split(" ")[2] is . This may look like a normal hyphen, but it is in fact a different character from the normal hyphen: -.

You can see this by getting the ordinal value of each:

string.split(" ")[2].ord # => 8211
"-".ord # => 45

Therefore, you should be checking for equality with the unicode character \u2013:

string.split(" ")[2] == "\u2013" # => true

Or you can replace all occurrences of \u2013 with -:

string.gsub!("\u2013", "-")
string.split(" ")[2] == "-" # => true
0
Zombo On

They are not equal

irb(main):002:0> string.split(" ")[2]
=> "[G–"
0
Sherwin Yu On

You're comparing two different dash characters. The normal dash that usually is inputed with the keyboard (ascii character 45) is what you're comparing to, but the text you're parsing has ascii character 8211 (the en dash).

See this http://www.ascii.cl/htmlcodes.htm.

Indeed, if you simply type "-" == "-", you'll get true