Skip to content

Instantly share code, notes, and snippets.

@rx
Last active December 7, 2016 10:04
Show Gist options
  • Save rx/3be8f1f0e05fbfafb1a93ab497076d2a to your computer and use it in GitHub Desktop.
Save rx/3be8f1f0e05fbfafb1a93ab497076d2a to your computer and use it in GitHub Desktop.
Command line math flash cards. Requires: Ruby 2 and thor Usage: `gem install thor && thor math:times`
Signal.trap("INT") do
puts ''
system('say "Goodbye!"')
exit 1
end
# A command line math flash cards
class Math < Thor
desc "times", "Practice your times tables - 2 to 12"
def times
@questions = 0
@wrong = 0
say "Press q to quit"
numbers.each do |left|
numbers.each do |right|
what_is?(left, right)
end
end
goodbye
end
no_commands do
CORRECT_ANSWERS = ['Yes Sir!',
'True Dat!',
'You know it!',
'Look at the big brain on you!',
'Keep it going!'].freeze
WRONG_ANSWERS = ['Not today!',
'Try again!',
'Nope',
'Keep trying',
'THe world needs ditch diggers too'].freeze
def numbers
[*2..12].shuffle
end
def what_is?(left, right)
loop do
answer = ask?(left, right)
break if correct?(answer, left, right)
wrong!
end
end
def ask?(left, right)
answer = ask "What is #{left} X #{right}?"
goodbye if answer.downcase=='q'
@questions += 1
answer
end
def correct?(answer, left, right)
correct = answer.to_i == left*right
system("say \"#{CORRECT_ANSWERS.sample}\"") if correct
correct
end
def wrong!
system("say \"#{WRONG_ANSWERS.sample}\"")
@wrong +=1
end
def goodbye
say "You answered #{@questions} times and had #{@questions-@wrong} correct." if @questions > 0
system('say "Goodbye!"')
exit
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment