Skip to content

Instantly share code, notes, and snippets.

@taylorlapeyre
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save taylorlapeyre/9469158 to your computer and use it in GitHub Desktop.

Select an option

Save taylorlapeyre/9469158 to your computer and use it in GitHub Desktop.
Quick attempt at Eric's thing in ruby, without dealing with arrays
class SpanishTutor
def initialize(path_to_dictionary)
@dictionary = parse_file(path_to_dictionary)
@num_incorrect = 0
@num_correct = 0
start_repl
end
def start_repl
loop do
correct = false
until correct
word = @dictionary.keys.sample
puts word
answer = gets.chomp
correct = grade(word, answer)
end
end
end
def grade(word, answer)
result = false
if @dictionary[word] == answer
result = true
@num_correct += 1
puts "Correct!"
elsif answer == 'q'
puts "You got #@num_correct correct."
exit
else
@num_incorrect += 1
puts "Try again!"
end
result
end
def parse_file(path_to_file)
dictionary = {}
File.open(path_to_file, "r").each do |line|
spanish_word, english_word = line.match(/(\w+) - (\w+)/).captures
dictionary[spanish_word] = english_word
end
dictionary
end
end
tutor = SpanishTutor.new('./dictionary.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment