Skip to content

Instantly share code, notes, and snippets.

@al-the-x
Created May 8, 2010 19:16
Show Gist options
  • Save al-the-x/394720 to your computer and use it in GitHub Desktop.
Save al-the-x/394720 to your computer and use it in GitHub Desktop.
class MoneyParser
ONE_TO_TEN = {
"one" => 1,
"two" => 2,
"nine" => 9,
"five" => 5,
"four" =>4,
"seven" => 7,
"eight" => 8,
"six" => 6,
"three" => 3,
"ten" => 10,
}
ELEVEN_TO_NINETEEN = {
"eleven" => 11,
"twelve" => 12,
"thirteen" => 13,
"fourteen" => 14,
"fifteen" => 15,
"sixteen" => 16,
"seventeen" => 17,
"eighteen" => 18,
"nineteen" => 19,
}
TWENTY_TO_NINETY_BY_TEN = {
"twenty" => 20,
"thirty" => 30,
"forty" => 40,
"fifty" => 50,
"sixty" => 60,
"seventy" => 70,
"eighty" => 80,
"ninety" => 90
}
def self.parse(string)
string = string.gsub(/ dollar(s)?/i, '')
if string.match(/\W/)
total = 0
string.split(/\W/).each do |string|
total += self.parse(string)
end
return total
else
map[string]
end
end
def self.map
{}.
merge(ONE_TO_TEN).
merge(ELEVEN_TO_NINETEEN).
merge(TWENTY_TO_NINETY_BY_TEN)
end
end #MoneyParser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment