Skip to content

Instantly share code, notes, and snippets.

@joshblack
Created March 5, 2014 07:20
Show Gist options
  • Save joshblack/9362657 to your computer and use it in GitHub Desktop.
Save joshblack/9362657 to your computer and use it in GitHub Desktop.
Simple Flashcard Demonstration, learning Ruby syntax.
module Flashcards
class Application
def initialize
@decks = []
end
def << deck
@decks << deck
end
def play
display_decks
puts "Pick a deck: "
deck = get_deck
deck.play
end
def display_decks
@decks.each { |deck| puts deck.name }
end
def get_deck
name = gets.chomp
@decks.detect { |deck| deck.name == name }
end
end
class Card
attr_accessor :front, :back
def initialize(front,back)
@front = front
@back = back
end
def correct? guess
guess == @back
end
def play
puts "#{front} > "
guess = gets.chomp
if correct? guess
puts "correct"
else
puts "Incorrect. The answer was #{back}"
end
end
end
class MultipleAnswerCard < Card
def correct? guess
answers = @back.split(",")
answers.any? { |answer| answer == guess }
end
end
class Deck
attr_reader :cards, :name
def initialize name
@name = name
@cards = []
end
def << card
@cards << card
end
def shuffle
@cards.shuffle!
end
def play
shuffle
@cards.each(&:play)
puts "playing the #{name} deck."
end
end
end
card1 = Flashcards::Card.new("cat", "neko")
card2 = Flashcards::Card.new("dog", "inu")
card3 = Flashcards::Card.new("snake", "hebi")
card = Flashcards::MultipleAnswerCard.new("Violin", "baoirin,viiorin")
deck = Flashcards::Deck.new("Japanese")
deck << card
deck << card1
deck << card2
deck << card3
deck2 = Flashcards::Deck.new("Russian")
app = Flashcards::Application.new
app << deck
app << deck2
app.play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment