Skip to content

Instantly share code, notes, and snippets.

@aliang
Created September 2, 2014 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aliang/037b274589ee161b2658 to your computer and use it in GitHub Desktop.
Save aliang/037b274589ee161b2658 to your computer and use it in GitHub Desktop.
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