# phone_letters_to_numbers class String # Converts a phone number with letters to numbers # Ex. AAA-FFFF => 222-3333 def convert_to_phone_number letters = 'abcdefghijklmnopqrstuvwxyz' numbers = '22233344455566677778889999' self.downcase.tr(letters, numbers) end # Converts the given string to the sequence of # numbers that you would need to push to create # that string. Shift = *, Space = # # Input: "Hello hi" # Output: "*44 33 555 555 666 # 44 444" def how_to_text letters = 'abcdefghijklmnopqrstuvwxyz ' numbers = '22233344455566677778889999#' multiplier = '123123123123123123412312341' str = '' self.each_byte do |char| char = char.chr if char =~ /[A-Z]/ str += '*' char.downcase! end str += if letters.include? char index = letters.index(char) numbers[index].chr * multiplier[index].chr.to_i else char end + " " end str.chop end # Converts the given numeric sequence back to # a text string. # Input: "*44 33 555 555 666 # 44 444" # Output: "Hello hi" def decode_text letters = 'abcdefghijklmnopqrstuvwxyz ' numbers = '22233344455566677778889999#' str = '' self.split.each do |part| shift = part[0].eql?(?*) ? true : false part = part[1,part.length] if shift pos = numbers.index(part) + (part.length-1) str += if shift letters[pos].chr.upcase else letters[pos].chr end end str end end