Skip to content

Instantly share code, notes, and snippets.

@madeindjs
Created May 12, 2019 09:40
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 madeindjs/2b0ff02784c7315b1dca50a1ea875718 to your computer and use it in GitHub Desktop.
Save madeindjs/2b0ff02784c7315b1dca50a1ea875718 to your computer and use it in GitHub Desktop.
Juste quelques améliorations au niveau des nommages
# Il vaut mieux définir des constantes (aka des variables qui ne changerons jamais)
# en dehors de la méthode. Les constantes sont juste nomées en majuscule
#
# La syntaxe avec le `%w[]` permet simplement de définir des tableaux en mode un peu plus sexy
NUMBER_TRANSLATIONS = %w[one two three four five six seven eight nine].freeze
DOZEN_TRANSLATIONS = %w[ten twenty thirty forty fifty sixty seventy eighty ninety].freeze
TEENAGERS = %w[eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen].freeze
# Convert a number to a beautiful string
# @param [Integer] as a number
# @return [String] as a pretty formated number
def english_number(number)
return 'Please enter a number that isn\'t negative.' if number < 0 # No negative numbers.
return 'zero' if number == 0
# The string we will return.
num_string = ''
# how much of the number we still have left to write out.
rest_number = number
# How many hundreds left to write out?
number_of_hundred = rest_number / 100
# Subtract off those hundreds.
rest_number -= number_of_hundred * 100
if number_of_hundred > 0
# Now here's a really sly trick:
hundreds = english_number number_of_hundred
num_string = num_string + hundreds + ' hundred'
# That's called "recursion". So what did I just do?
# I told this method to call itself, but with "write" instead of
# "number". Remember that "write" is (at the moment) the number of
# hundreds we have to write out. After we add "hundreds" to
# "num_string", we add the string ' hundred' after it.
# So, for example, if we originally called english_number with
# 1999 (so "number" = 1999), then at this point "write" would
# be 19, and "left" would be 99. The laziest thing to do at this
# point is to have english_number write out the 'nineteen' for us,
# then we write out ' hundred', and then the rest of
# english_number writes out 'ninety-nine'.
if rest_number > 0
# So we don't write 'two hundredfifty-one'...
num_string += ' '
end
end
number_of_tens = rest_number / 10 # How many tens left to write out?
rest_number -= number_of_tens * 10 # Subtract off those tens.
if number_of_tens > 0
if (number_of_tens == 1) && (rest_number > 0)
# Since we can't write "tenty-two" instead of "twelve",
# we have to make a special exception for these.
num_string += TEENAGERS[rest_number - 1]
# The "-1" is because TEENAGERS[3] is 'fourteen', not 'thirteen'.
# Since we took care of the digit in the ones place already,
# we have nothing left to write.
rest_number = 0
else
num_string += DOZEN_TRANSLATIONS[number_of_tens - 1]
# The "-1" is because DOZEN_TRANSLATIONS[3] is 'forty', not 'thirty'.
end
if rest_number > 0
# So we don't write 'sixtyfour'...
num_string += '-'
end
end
if rest_number > 0
num_string += NUMBER_TRANSLATIONS[rest_number - 1]
# The "-1" is because NUMBER_TRANSLATIONS[3] is 'four', not 'three'.
end
# Now we just return "num_string"...
num_string
end
puts 'Plz enter a number'
user_input = gets.to_i
puts english_number user_input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment