Skip to content

Instantly share code, notes, and snippets.

@trivett
Created November 29, 2013 18:30
Show Gist options
  • Save trivett/7709980 to your computer and use it in GitHub Desktop.
Save trivett/7709980 to your computer and use it in GitHub Desktop.
Command line program for testing your counting in a foreign language. Feel free to add a new language and build up the total list of words as far as desired. Want to throw in Swedish? Have at it. Care to add numbers up to 100 in French? Just put them in order.
puts "Welcome to the Number Vocabulary Test Program"
puts "A simple program for learning your numbers in a foreign language."
words = {
"Japanese" => ['zero','ichi','ni','san','yon','go','roku','shichi','hachi','kyuu','juu','juuichi','juuni','juusan'],
"Spanish" => ['cero','uno','dos','tres','cuatro','cinco','seis','siete','ocho','nueve','diez'],
"German" => ['null','eins','zwei','drei','vier','funf','sechs','sieben','acht','neun', 'zehn']
}
def play(words)
lang = nil
while lang.nil?
puts "Choose a language from the following list:"
puts words.keys
choice = gets.chomp.downcase.capitalize!
if words.keys.include?(choice)
lang = choice
else
puts "Sorry, please press enter then type the language you wish to practice."
choice = gets.chomp.downcase.capitalize!
end
end
puts "Right on, let's practice numbers in #{lang.upcase}!"
puts "Type the numeral that corresponds to the foreign language word displayed."
ind = words[lang].to_a
nums = (0..ind.length).to_a
quiz = Hash[ind.zip(nums.map)] # This hash has key value pairs of the words and the corresponding numerals.
bestscore = quiz.length
score = 0
while quiz.length > 0
quiz.sort_by { rand }.each do |k,v|
puts "What does the #{lang} word #{k} means in English?"
if gets.chomp.to_i == v
puts "Correct."
quiz.delete(k)
# Get it right, and it comes out of the hash and you don't have to practice it again. Get it wrong, and it stays in the shuffle. Repetition helps you memorize the words you need to work on.
puts "#{quiz.length} more left."
else
puts "Incorrect. The correct answer is #{v}. This will come back up again until you get it right."
# This way even if you start out knowing nothing, you can learn as you go along and still eventually pass.
end
score += 1
end
end
puts "It took you #{score.to_s} tries to get through #{bestscore.to_s} vocab words."
if score > bestscore
puts "Better luck next time."
else
puts "Congratulations, you know your numbers in #{lang}"
end
lang
end
play(words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment