Skip to content

Instantly share code, notes, and snippets.

@Dawenster
Last active December 14, 2015 10:39
Show Gist options
  • Save Dawenster/5074102 to your computer and use it in GitHub Desktop.
Save Dawenster/5074102 to your computer and use it in GitHub Desktop.
Run this in terminal if you want some practice on ruby terms and definitions! Notes: - Put everything in the same folder - Typing 'exit' leaves the program - Typing 'show statistics' shows you your current statistics
class Card
attr_reader :question, :answer, :times_guessed
attr_accessor :status
def initialize(args)
@question = args[:question]
@answer = args[:answer]
@status = :not_attempted
@times_guessed = 0
end
def correct?(current_card, response)
current_card.answer == response
end
def update_correct(current_card)
current_card.status = :correct
@times_guessed = 0
end
def update_count(current_card)
@times_guessed += 1
end
def update_incorrect(current_card, response)
current_card.status = :incorrect unless current_card.correct?(current_card, response)
@times_guessed = 0
end
end
class Deck
attr_reader :not_attempted_cards, :correct_cards, :incorrect_cards
def initialize(list_of_cards)
@list_of_cards = list_of_cards
@not_attempted_cards = []
@correct_cards = []
@incorrect_cards = []
end
def initialize_deck
@list_of_cards.each { |card| @not_attempted_cards << card }
end
def shuffle_cards
@not_attempted_cards.shuffle!
@correct_cards.shuffle!
@incorrect_cards.shuffle!
end
def organize_cards
@not_attempted_cards.each do |card|
case
when card.status == :correct then @correct_cards << card
when card.status == :incorrect then @incorrect_cards << card
end
end
end
def delete_card(current_card)
@not_attempted_cards.delete(current_card)
end
end
class Game
NUM_GUESSES_ALLOWED = 3
attr_reader :current_card, :response, :deck
def initialize(deck)
@deck = deck
@current_card = ''
@response = ''
@view = Messages.new
end
def run
deck.initialize_deck
until deck.not_attempted_cards.length == 0
deck.shuffle_cards
post_question
store_response
check_answer
update_incorrect
deck.organize_cards unless entered_show_statistics?
deck.delete_card(@current_card) unless entered_show_statistics?
end
@view.statistics(deck.correct_cards, deck.incorrect_cards)
end
def post_question
@current_card = deck.not_attempted_cards.first
@view.post_question(deck.not_attempted_cards, @current_card)
end
def store_response
@view.ask_user_for_response
@response = gets.chomp
exit if @response.downcase == 'exit'
end
def entered_show_statistics?
@response.downcase == 'show statistics'
end
private
def check_answer
return @view.statistics(deck.correct_cards, deck.incorrect_cards) if entered_show_statistics?
until @current_card.status == :correct || @current_card.times_guessed == NUM_GUESSES_ALLOWED - 1
if @current_card.correct?(@current_card, @response)
@view.positive_encouragement
@current_card.update_correct(@current_card)
else
@view.wrong_message(@current_card)
@current_card.update_count(@current_card)
store_response
end
end
end
def update_incorrect
return if entered_show_statistics?
@current_card.update_incorrect(@current_card, @response)
@view.still_wrong(@current_card, @response)
end
end
require_relative 'model'
require_relative 'view'
require_relative 'controller - card'
require_relative 'controller - game'
require_relative 'controller - deck'
loaded_questions = ParseFile.new
deck = Deck.new(loaded_questions.run)
game = Game.new(deck)
game.run
class ParseFile
attr_accessor :list_of_cards
def initialize
@lines = []
@nested_array = []
@nested_array_with_keys = []
@list_of_cards = []
end
def run
load_questions
make_nested_arrays
make_cards
end
def load_questions
File.open('questions').each { |line| @lines << line.chomp unless line.chomp == '' }
end
def make_nested_arrays
keys = [:question, :answer]
@lines.each_slice(2).to_a.each { |array| @nested_array_with_keys << keys.zip(array) }
end
def make_cards
@nested_array_with_keys.each { |array| list_of_cards << Card.new(Hash[*array.flatten]) }
list_of_cards
end
end
To create a second name for the variable or method.
alias
A command that appends two or more objects together.
and
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.
begin
Gives an unconditional termination to a code block, and is usually placed with an argument.
break
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.
case
Opens a class definition block, which can later be reopened and added to with variables and even functions.
class
Used to define a function.
def
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.
defined?
Paired with end, this can delimit a code block, much like curly braces; however, curly braces retain higher precedence.
do
Gives an "otherwise" within a function, if-statement, or for-loop, i.e. if cats = cute, puts "Yay!" else puts "Oh, a cat."
else
Much like else, but has a higher precedence, and is usually paired with terms.
elsif
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.
end
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.
ensure
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.)
false
A loop constructor; used in for-loops.
for
Ruby's basic conditional statement constructor.
if
Used with for, helps define a for-loop.
in
Opens a library, or module, within a Ruby Stream.
module
Bumps an iterator, or a while or until block, to the next iteration, unconditionally and without executing whatever may remain of the block.
next
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).
nil
Boolean negation. i.e. not true # false, not 10 # false, not false # true.
not
Boolean or. Differs from || in that or has lower precedence.
or
Causes unconditional re-execution of a code block, with the same parameter bindings as the current execution.
redo
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).
rescue
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.
retry
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.
return
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.
self
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.
super
Optional component of conditional statements (if, unless, when). Never mandatory, but allows for one-line conditionals without semi-colons.
then
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.
true
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.
undef
The negative equivalent of if. i.e. unless y.score > 10 puts "Sorry; you needed 10 points to win." end.
unless
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.
until
Same as case.
when
Takes a condition argument, and executes the code that follows (up to a matching end delimiter) while the condition is true.
while
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.
yield
class Messages
ASTERIX_LINE = '*' * 60
NUM_GUESSES_ALLOWED = 3
def post_question(not_attempted_cards, current_card)
puts "You have #{not_attempted_cards.length} questions remaining."
puts ASTERIX_LINE
puts 'Definition:'
puts current_card.question.capitalize
puts ASTERIX_LINE
end
def ask_user_for_response
puts 'Your answer:'
end
def positive_encouragement #shoutout to Chris Malin for this idea
puts ['Good job!', 'You are a superstar!', 'OMG are you a genius?', 'Keep going you sexy thang!'].sample
end
def wrong_message(current_card)
puts "WRONG! (#{NUM_GUESSES_ALLOWED - 1 - current_card.times_guessed} remaining...)"
end
def still_wrong(current_card, response)
puts "STILL WRONG! The answer was '#{current_card.answer}'. Moving on..." unless current_card.correct?(current_card, response)
puts ASTERIX_LINE
end
def statistics(correct_cards, incorrect_cards)
puts ASTERIX_LINE
puts "You answered #{correct_cards.length} card(s) correctly."
puts "You answered #{incorrect_cards.length} card(s) incorrectly."
puts ASTERIX_LINE
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment