Skip to content

Instantly share code, notes, and snippets.

@dpk
Created June 2, 2012 07:09
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 dpk/2857091 to your computer and use it in GitHub Desktop.
Save dpk/2857091 to your computer and use it in GitHub Desktop.
txt2num: convert a 'telephone word' into raw numbers
#!/usr/bin/env ruby
def txt2num str
letters = {'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}
str.chars.map do |char|
letters[char.downcase] || char
end.join
end
if ARGV.empty?
while gets
print txt2num $_
end
else
ARGV.each {|word| puts txt2num word }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment