Skip to content

Instantly share code, notes, and snippets.

@aereal
Created Jun 11, 2010
Embed
What would you like to do?
class Integer
def english_name(delimiter='-')
if self < 20
english_table[self]
else
ret = english_table[(self / 10) * 10]
(self % 10) == 0 ? ret : ret + delimiter.to_s + english_table[self % 10]
end
end
private
def english_table
{ 0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'nineteen',
20 => 'twenty',
30 => 'thirty',
40 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
}
end
end
puts 13.english_name # => thirteen
puts 40.english_name # => fourty
puts 33.english_name # => thirty-three
puts 28.english_name("_") # => twenty_eight
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment