Skip to content

Instantly share code, notes, and snippets.

@ramoncaldeira
Last active August 29, 2015 14:11
Show Gist options
  • Save ramoncaldeira/af2ce5344a5a22382aca to your computer and use it in GitHub Desktop.
Save ramoncaldeira/af2ce5344a5a22382aca to your computer and use it in GitHub Desktop.
UNITS = Hash[(1..9).zip %w(um dois tres quatro cinco seis sete oito nove)]
SPECIAL = Hash[(11..19).zip %w(onze doze treze quatorze quinze dezesseis dezessete dezoito dezenove)]
DOZENS = Hash[(1..9).zip %w(dez vinte trinta quarenta cinquenta sessenta setenta oitenta noventa)]
def to_words(number)
return SPECIAL[number] if number.between?(11, 19)
dozens, units = number.divmod(10)
[DOZENS[dozens], UNITS[units]].compact.join(' e ')
end
# main program
(1..99).each do |n|
puts to_words(n)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment