Csteele5 Csteele5 - 1 year ago 129
Ruby Question

Get difference between two character codes in Ruby

Although it might not be readable, I am hoping that someone can tell me if it is possible - and if so how - to make the following code work

true if (string1 - string2) == predefined_distance

I would hope this might be possible using Ruby's Open Classes, but looking at the documentation for UTF-8 and
I'm not sure if it's actually possible to do do anything more than
which isn't at all precise. Perhaps we can convert to ASCII and compare those values?

An example of use might be to compare two opening an closing brackets, to see if they match instead of defining separate lists or containers for each type of bracket, and instead we can just define a distance for any two arbitrary characters.

Answer Source

Built-in method ord of String class can return number of the character:

true if (string1.chars.first.ord - string2.chars.first.ord) == predefined_distance