Search code examples
rubystringintegeralphabet

Strings to Integers


Say you have the string "Hi". How do you get a value of 8, 9 ("H" is the 8th letter of the alphabet, and "i" is the 9th letter). Then say, add 1 to those integers and make it 9, 10 which can then be made back into the string "Ij"? Is it possible?


Solution

  • use ord to get the ASCII index, and chr to bring it back.

    'Hi'.chars.map{|x| (x.ord+1).chr}.join