Ruby Flare

Your awesome Tagline

0 notes

Converting numbers and letters

When dealing with legacy data I often find annoying things like integers used to represent letters, integers stored as strings, or both.

"1", "2", "3", "4", "5" => "A", "B", "C", "D", "E"

In ruby 1.9, if you want to convert a number that is stored as a string to its corresponding capital letter:

("1".to_i + 64).chr    # => "A"

To go back the other way:

("A".ord - 64).to_s    # => "1"

Filed under ruby legacy