I've built a simple command line flashcard game for learning Korean. I'm a beginner programmer, so I just wanted to make sure I'm following best practices, especially pertaining to OOP principles.

Any feedback would be greatly appreciated.

Here's GitHub link and reposted below:

class FlashCard

  def initialize
    # TODO: load from file
    @korean_dict = {
      "사과" => "apple",
      "맥주" => "beer",
      "책" => "book",
      "의자" => "chair",
      "피아노" => "piano"
    @english_dict = @korean_dict.invert
    @correct = 0

  # starts the game
  def play
    puts '----------------'
    puts '----------------'

    dict = pick_dict

  # select korean-to-english or english-to-korean dictionary
  def pick_dict
    user_input = -1
    until (user_input == 1 || user_input == 2)
        print "Korean to English [press 1] or English to Korean [press 2]: " 
        user_input = gets.chomp.to_i
    user_input == 1 ? @korean_dict : @english_dict

  # loop through dictionary and prompt user for answers
  def ask_questions(dict)
    dict.each_with_index do |(key, val), i|   
      print "Question #{i+1}: #{key} is "
      input = gets.chomp
      check_answer(input, val, i)    

  # check if user input matches correct translation
  def check_answer(input, val, index)
    if (input == val)
      @correct += 1
      puts "Correct! #{@correct} of #{index+1} correct.\n\n" 
      print "Sorry, the correct answer is #{val}." 
      print " #{@correct} of #{index+1} correct.\n\n"

  def print_results(dict)
    puts "-------------------------------------"
    puts "Your final score: #{@correct} of #{dict.length}."
    puts "Below is a list of words in the quiz: "
    dict.each { |key, val| puts "#{key} --- #{val}" }
    puts "-------------------------------------\n\n"


# Runs Program

fc = FlashCard.new
  • 3
    \$\begingroup\$ +1 for technology and learning! Another theoretical +1 for reminding me of dictionary reverse (I am building a French linguistics software) :) \$\endgroup\$ Jul 21, 2014 at 17:54

1 Answer 1


The program's data structures aren't necessarily complicated enough for OOP - you'll almost never need to create multiple instances of any of the potential objects. So don't worry about it too much.

The only big problem I can see is that you always calculate both the Korean-English and English-Korean dictionaries even though only one is used each game, which could be a big waste of memory if the dictionaries get larger. I'd suggest waiting until after the user has chosen their direction and THEN invert the hash if you need to, and ideally throw away the uninverted version (unfortunately Ruby doesn't have an invert! method on hashes). If you're reading from a file, you can invert the data yourself as it is read in.

The one OOP improvement I'd recommend is to wrap the dictionary in an abstract data type. Rather than counting on it being a hash, write methods that return a word to use as a question and the translation of input word, and call those. That way, when you want to make the dictionary bigger or read it from a file, you can just modify those methods. For example, when the dictionary gets really big you might want to read it from disk by random access rather than reading the whole thing into memory. Or you could even write a version that makes requests to a web service.

  • \$\begingroup\$ thanks for the feedback. i took your advice and ask user upfront which dictionary they want to use, so that i only load one dictionary. \$\endgroup\$
    – Jamie B
    Jul 24, 2014 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.