Skip to content

Instantly share code, notes, and snippets.

@scottchiang
Forked from dbc-challenges/card.rb
Created October 25, 2012 17:48
Show Gist options
  • Save scottchiang/3954289 to your computer and use it in GitHub Desktop.
Save scottchiang/3954289 to your computer and use it in GitHub Desktop.
FlashCardinator
class Flashcard
attr_accessor :word, :definition
def initialize(word, definition)
@word = word
@definition = definition
end
end
class CardStack
attr_accessor :deck, :index
FILENAME = 'flashcards_data.txt'
def initialize
@deck = []
@index = 0
end
def load_file
@file_loaded = File.open(FILENAME).read.to_s.split("\n")
end
def create_cards
@file_loaded.each do |elem|
@deck << Flashcard.new(elem.split("\t").first, elem.split("\t").last)
end
end
def next_card
@index += 1
end
def show_word
"#{@deck[@index].word}"
end
def show_definition
"#{@deck[@index].definition}"
end
def quit
@index = 0
end
def guess_definition(word)
return true if word == show_word
return false
end
end
class User
attr_accessor :stack
def initialize
@stack = CardStack.new ### initialize with a new CardStack class
@stack.load_file
@stack.create_cards
end
def show_word
@stack.show_word
end
def show_definition
@stack.show_definition
end
def guess(word)
@stack.guess_definition(word)
end
def next_card
@stack.next_card
end
def quit
@stack.quit
end
end
class Session
def initialize
@scott = User.new
@guess = 0
end
def display_card
puts "#{@scott.show_definition} \n"
end
def start_session
puts "Welcome to a new flash card session."
puts "Please enter the word that corresponds with the definitions."
puts "Type next to move on to the next word"
puts "Type stop to exit the program"
puts "Type start to begin"
input = gets.chomp
until input == "stop"
display_card
input = gets.chomp
if input == @scott.show_word
puts "Correct!! \n"
@scott.next_card
elsif input != @scott.show_word && @guess != 2 && input != "next"
puts "Wrong, Try again \n"
@guess += 1
elsif @guess == 2
puts "You failed to guess the correct word after 3 tries. \n"
puts "The correct word is '#{@scott.show_word}' \n"
@scott.next_card
@guess = 0
elsif input == "next"
@scott.next_card
elsif input == "quit"
break
end
end
end
end
sess = Session.new
sess.start_session
require 'simplecov'
SimpleCov.start
require './flashcardinator.rb'
require 'rspec'
describe Flashcard do
context "Creating a new class" do
context "When a word and definition is provided" do
it "uses the word and definition" do
word = "else"
definition = "gives an otherwise within a function"
fc = Flashcard.new(word, definition)
fc.word.should eq word
fc.definition.should eq definition
end
end
end
end
describe CardStack do
context "Creating a new class" do
it "initializes the class as an array" do
stack = CardStack.new
stack.deck.should be_a_kind_of Array
end
it "initializes the index at 0" do
stack = CardStack.new
stack.index.should eq 0
end
end
context "Loading a text file" do
it "loads a text file and turns it into an array" do
stack = CardStack.new
stack.load_file.should be_a_kind_of Array
end
end
context "Validate cards" do
it "checks to see if all the words only have 1 word" do
stack = CardStack.new
stack.load_file
stack.create_cards
stack.deck.each do |elem|
elem.word.should_not include " "
end
end
end
context "Fills the CardStack with Flashcard objects using info from the text file" do
it "creates Flashcard objects and puts it into the deck" do
stack = CardStack.new
stack.load_file
stack.create_cards
card = stack.deck.first
card.word.should eq "alias"
card.definition.should eq "To create a second name for the variable or method."
end
end
context "Moves on to the next card" do
it "increases the index by 1" do
stack = CardStack.new
stack.next_card.should eq 1
end
end
context "Show the current word" do
it "shows the current word based on the index" do
stack = CardStack.new
stack.load_file
stack.create_cards
stack.show_word.should eq "alias"
end
end
context "Show the definition of current word" do
it "shows the definition that corresponds with the current word" do
stack = CardStack.new
stack.load_file
stack.create_cards
stack.show_definition.should eq "To create a second name for the variable or method."
end
end
context "Quit using the flashcards" do
it "the flashcards start over at the first card" do
stack = CardStack.new
stack.load_file
stack.create_cards
stack.next_card
stack.quit
stack.index.should eq 0
end
end
context "Guess the word" do
it "asks the user to guess the word given a definition" do
stack = CardStack.new
stack.load_file
stack.create_cards
#user enters a guess via gets.chomp
word = "alias"
stack.guess_definition(word).should == true
word = "and"
stack.guess_definition(word).should == false
end
end
end
describe User do
context "Creating a new class" do
it "gives the user a stack of flashcards upon intialization" do
scott = User.new
scott.stack = CardStack.new
scott.stack.should_not be_nil
end
end
context "Show user a definition" do
it "shows the user the first definition from the stack of flashcards" do
scott = User.new
scott.stack = CardStack.new
scott.stack.load_file
scott.stack.create_cards
scott.stack.show_definition.should eq "To create a second name for the variable or method."
end
end
context "Go to next card" do
it "displays the next card in the flashcard stack" do
scott = User.new
scott.stack = CardStack.new
scott.stack.load_file
scott.stack.create_cards
scott.next_card.should eq 1
end
end
context "#quit" do
it "goes back to the very first flashcard" do
scott = User.new
scott.stack = CardStack.new
scott.stack.load_file
scott.stack.create_cards
scott.next_card
scott.quit.should eq 0
end
end
context "#show_word" do
it "shows the user the word of the current card" do
scott = User.new
scott.stack.load_file
scott.stack.create_cards
scott.show_word.should eq "alias"
end
end
context "#show_definition" do
it "shows the user the definition of the current card" do
scott = User.new
scott.stack.load_file
scott.stack.create_cards
scott.show_definition.should eq "To create a second name for the variable or method."
end
end
context "#guess" do
it "allows the user to guess what the correct word is" do
scott = User.new
scott.stack.load_file
scott.stack.create_cards
word = "computer"
scott.guess(word).should eq false
end
end
end
alias To create a second name for the variable or method.
and A command that appends two or more objects together.
BEGIN Designates code that must be run unconditionally at the beginning of the program before any other.
begin Delimits a "begin" block of code, which can allow the use of while and until in modifier position with multi-line statements.
break Gives an unconditional termination to a code block, and is usually placed with an argument.
case starts a case statement; this block of code will output a result and end when it's terms are fulfilled, which are defined with when or else.
class Opens a class definition block, which can later be reopened and added to with variables and even functions.
def Used to define a function.
defined? A boolean logic function that asks whether or not a targeted expression refers to anything recognizable in Ruby; i.e. literal object, local variable that has been initialized, method name visible from the current scope, etc.
do Paired with end, this can delimit a code block, much like curly braces; however, curly braces retain higher precedence.
else Gives an "otherwise" within a function, if-statement, or for-loop, i.e. if cats = cute, puts "Yay!" else puts "Oh, a cat."
elsif Much like else, but has a higher precedence, and is usually paired with terms.
END Designates, via code block, code to be executed just prior to program termination.
end Marks the end of a while, until, begin, if, def, class, or other keyword-based, block-based construct.
ensure Marks the final, optional clause of a begin/end block, generally in cases where the block also contains a rescue clause. The code in this term's clause is guaranteed to be executed, whether control flows to a rescue block or not.
false denotes a special object, the sole instance of FalseClass. false and nil are the only objects that evaluate to Boolean falsehood in Ruby (informally, that cause an if condition to fail.)
for A loop constructor; used in for-loops.
if Ruby's basic conditional statement constructor.
in Used with for, helps define a for-loop.
module Opens a library, or module, within a Ruby Stream.
next Bumps an iterator, or a while or until block, to the next iteration, unconditionally and without executing whatever may remain of the block.
nil A special "non-object"; it is, in fact, an object (the sole instance of NilClass), but connotes absence and indeterminacy. nil and false are the only two objects in Ruby that have Boolean falsehood (informally, that cause an if condition to fail).
not Boolean negation. i.e. not true # false, not 10 # false, not false # true.
or Boolean or. Differs from || in that or has lower precedence.
redo Causes unconditional re-execution of a code block, with the same parameter bindings as the current execution.
rescue Designates an exception-handling clause that can occur either inside a begin<code>/<code>end block, inside a method definition (which implies begin), or in modifier position (at the end of a statement).
retry Inside a rescue clause, causes Ruby to return to the top of the enclosing code (the begin keyword, or top of method or block) and try executing the code again.
return Inside a method definition, executes the ensure clause, if present, and then returns control to the context of the method call. Takes an optional argument (defaulting to nil), which serves as the return value of the method. Multiple values in argument position will be returned in an array.
self The "current object" and the default receiver of messages (method calls) for which no explicit receiver is specified. Which object plays the role of self depends on the context.
super Called from a method, searches along the method lookup path (the classes and modules available to the current object) for the next method of the same name as the one being executed. Such method, if present, may be defined in the superclass of the object's class, but may also be defined in the superclass's superclass or any class on the upward path, as well as any module mixed in to any of those classes.
then Optional component of conditional statements (if, unless, when). Never mandatory, but allows for one-line conditionals without semi-colons.
true The sole instance of the special class TrueClass. true encapsulates Boolean truth; however, <emph>all</emph> objects in Ruby are true in the Boolean sense (informally, they cause an if test to succeed), with the exceptions of false and nil.
undef Undefines a given method, for the class or module in which it's called. If the method is defined higher up in the lookup path (such as by a superclass), it can still be called by instances classes higher up.
unless The negative equivalent of if. i.e. unless y.score > 10 puts "Sorry; you needed 10 points to win." end.
until The inverse of while: executes code until a given condition is true, i.e., while it is not true. The semantics are the same as those of while.
when Same as case.
while Takes a condition argument, and executes the code that follows (up to a matching end delimiter) while the condition is true.
yield Called from inside a method body, yields control to the code block (if any) supplied as part of the method call. If no code block has been supplied, calling yield raises an exception.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment