Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Vanity phone number converter
# Converts a phone number with letters to a phone number with only numbers.
# Leaves any other characters untouched.
class VanityPhoneNumberConverter
LETTER_TO_NUMBER = {
'a' => '2', 'b' => '2', 'c' => '2',
'd' => '3', 'e' => '3', 'f' => '3',
'g' => '4', 'h' => '4', 'i' => '4',
'j' => '5', 'k' => '5', 'l' => '5',
'm' => '6', 'n' => '6', 'o' => '6',
'p' => '7', 'q' => '7', 'r' => '7', 's' => '7',
't' => '8', 'u' => '8', 'v' => '8',
'w' => '9', 'x' => '9', 'y' => '9', 'z' => '9'
}
attr_accessor :phone_number
def initialize(phone_number)
@phone_number = phone_number
end
def convert
result = ""
@phone_number.each_char do |c|
result << (LETTER_TO_NUMBER[c.downcase] || c)
end
result
end
def self.convert(markdown)
self.new(markdown).convert
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment