Last Updated: February 25, 2016
·
832
· kormie

"Foo" == "Foo" != true (Non printing characters FTL)

Today I ran into a particularly frustrating problem. Basically what was happening was this:

pry> Object.text
=> "Foo"
pry> Object.text == "Foo"
=> false

Obviously this made no sense. After a while I realized there may be a non-printing character which irb was hiding.

pry> Object.text.dump
=> "\"Foo\\u{200b}\""

Now I could see there was a zero width space being added to the string, causing the match to return false. After this, it was a simple matter of removing the unicode character:

Object.text.gsub!(/[\u200b]/,"")

After that:

pry> Object.text == "Foo"
=> true

Cheers, hope this helps!