Created
May 31, 2012 18:07
-
-
Save thoughtpunch/2845117 to your computer and use it in GitHub Desktop.
ToDo List example from Reddit thread:http://www.reddit.com/r/ruby/comments/udrkh/benchmarking_proficiency_in_ruby/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ToDoList | |
def initialize | |
puts "What's your name, good sir?" | |
name = gets.strip | |
puts greeting(name) #this uses the greeting below | |
puts "1.) Make A To-Do List \n2.) Load A To-Do List\n" | |
while do_action = gets.chomp | |
case do_action | |
when "1" | |
make_list #this should optionally take params, like name. | |
when "2" | |
get_list #This should ask for a file name that gets passed as a param to the 'get_list' method like get_list('grocery') | |
else | |
puts "I'm sorry, I don't understand. You can only enter from the options! :)" | |
end | |
end | |
end | |
def greeting(name) | |
unless name.nil? | |
case Time.now.hour | |
when (6...12) | |
time_phase = "morning" | |
when (12...16) | |
time_phase = "afternoon" | |
when (16...20) | |
time_phase = "evening" | |
when (20..24) || (1...6) | |
time_phase = "night" | |
end | |
return "\nGood #{time_phase}, Master #{name}! What would you like me to do?" | |
end | |
end | |
#OK...so these methods are terrible. I don't have time to refactor them and they should be broken up into multiple methods, one for each 'concern' or action | |
#Also, the if/case statements are a terrible mess. I'll finish the refactor later so you can see a better way to do this. | |
def make_list | |
puts "Okay! What name would you like to name the list?" | |
topic_header = gets.chomp.upcase | |
file = File.new("#{topic_header}.txt", 'w') | |
file << "////////////////////////// #{topic_header} ////////////////////////// \n" | |
puts "\nAlright, #{name}! \nTemme the list items!" | |
i = 0 | |
i2 = 0 | |
i3 = 0 | |
while input = gets | |
i += 1 | |
if input =~ /^END/ | |
# if i == 1 | |
# num_tasks = "task" | |
# elsif i == 2 | |
# num_tasks = "task" | |
# else | |
# num_tasks = "tasks" | |
# end | |
file << "\n----------------------------------------------------------------------------------------------------------------------" | |
file << "\n #{"#"*i} Main Tasks: \t Total: #{i - 1} Fin: 0 left: #{i - 1}" | |
file << "\n #{"#"*i} Sub Tasks: \t\t Total:#{i2} Fin:0 left:#{i2}" | |
file << "\n----------------------------------------------------------------------------------------------------------------------" | |
break | |
end | |
puts ">> " + input | |
file << "\n#{i}.) " + input.strip + " [_] \n" | |
puts "Would you like any other items under this? Please answer in yes or no." | |
do_action2 = gets.chomp | |
case do_action2.downcase | |
when 'yes' | |
i2 -= 1 | |
#i3 -= 1 | |
puts "Okay! Tell me then!" | |
while input2 = gets | |
i2 += 1 | |
#i3 += 1 | |
if input2 =~ /^END/ | |
#file << "\n\t#{i3} tasks, 0 finished, #{i3} left.\n" | |
#i3 = 0 | |
break | |
end | |
puts ">> \t" + input2 | |
file << "\t #{i2 + 1}-> " + input2.strip + " ||\n" | |
end | |
puts "Enter the next list item!" | |
when 'no' | |
puts "Okay, as you wish!" | |
puts "Enter the next list item then!" | |
else | |
puts "Awwwwhhh man...you shouldn't have done that. Program terminating in 3..2..1" | |
return | |
end | |
end | |
puts "Alrighto, #{name}o! Your to-do list has been prepared using Ruby magic. Goodbye for now! :D" | |
file.close | |
break | |
end | |
def get_list | |
puts "Which file would you like to read, bro?" | |
#1) you could even use the Ruby "File" lib here (in place of IO) to list files in | |
# the current dir and choose one. | |
#2) This is extremely error-prone. What if they enter a non-existent file? An integer? etc. | |
commnd = gets.strip | |
filelines = IO.readlines("#{commnd}.txt") | |
puts "\nYour file has been loaded, good sir. What would you like to do with it?\n\n" | |
# OK.. so there are at least 2 better ways to structure this: | |
# 1) Break the various actions into methods (i.e. "list_tasks","number_tasks_remaining",etc) | |
# and call them inside 'get_list' | |
# 2) Keep this code below as a method, but pass the action in as a parameter (i.e. list_action(params) ) | |
puts "1.) List the number of tasks left. \n2.) List each task. \n3.) List completed tasks. \n4.) List incomplete tasks. \n5.) Mark a task complete." | |
while do_action3 = gets.strip | |
case do_action3 | |
when "1" | |
if filelines[-3] =~ /Main Tasks/ | |
puts filelines[-3] | |
else | |
puts "Sorry, this file doesn't specify main tasks." | |
end | |
if filelines[-2] =~ /Sub Tasks/ | |
puts filelines[-2] | |
else | |
puts "Sorry, this file doesn't specify sub tasks." | |
end | |
break | |
when "2" | |
$task_lines = [] | |
def capture_task(val) | |
$task_lines << val | |
end | |
i4 = 0 | |
filelines.each do |line| | |
i4 += 1 | |
if line =~ /\.\)/ || line =~ /->/ | |
if line =~ /\.\)/ | |
puts "\n" + line | |
elsif line =~ /->/ | |
puts line | |
end | |
capture_task(line) | |
end | |
end | |
if $task_lines[0] == $task_lines[-1] | |
puts "No tasks found." | |
end | |
break | |
when "3" | |
$fin_lines = [] | |
def capture_fin(val) | |
$fin_lines << val | |
end | |
filelines.each do |fin_task| | |
if fin_task =~ /\[X\]/ || fin_task =~ /\|X\|/ | |
puts fin_task | |
capture_fin(fin_task) | |
#else | |
#puts "Looks like you haven't completed anything!" if fin_task == filelines[-1] | |
end | |
end | |
if $fin_lines[0] == $fin_lines[-1] && $fin_lines[0].class != String | |
puts "No completed tasks found!" | |
end | |
break | |
when "4" | |
$left_lines = [] | |
def capture_left(val) | |
$left_lines << val | |
end | |
filelines.each do |left_task| | |
if left_task =~ /\[_\]/ || left_task =~ /\|\|/ | |
puts left_task | |
capture_left(left_task) | |
#else | |
#puts "Looks like you've completed everything!" if left_task == filelines[-1] | |
end | |
end | |
if $left_lines[0] == $left_lines[-1] && $left_lines[0].class != String | |
puts "No incomplete tasks found!" | |
end | |
break | |
when "5" | |
puts "Which task would you like to mark as completed?" | |
filelines.each do |left_task| | |
puts left_task if left_task =~ /\[_\]/ || left_task =~ /\|\|/ | |
end | |
numenum = (total_tasks, fin_tasks, left_tasks = filelines[-3].scan(/\d+/).each) | |
subnumenum = (total_subtasks, fin_subtasks, left_subtasks = filelines[-2].scan(/\d+/).each) | |
total_tasks, total_subtasks = numenum.next.to_i, subnumenum.next.to_i | |
fin_tasks, fin_subtasks = numenum.next.to_i, subnumenum.next.to_i | |
left_tasks, left_subtasks = numenum.next.to_i, subnumenum.next.to_i | |
# puts total_tasks | |
# puts fin_tasks | |
# puts left_tasks | |
while do_action4 = gets.chomp | |
if do_action4 =~ /^END/ | |
file2 = File.new("#{commnd}.txt", "w") | |
file2.puts $capt_fin | |
break | |
else | |
if do_action4 =~ /\.\)/ | |
fin_tasks += 1 | |
left_tasks -= 1 | |
total_tasks = fin_tasks + left_tasks | |
elsif do_action4 =~ /->/ | |
fin_subtasks += 1 | |
left_subtasks -= 1 | |
total_subtasks = fin_subtasks + left_subtasks | |
end | |
puts "Do you want to mark another task as completed?" | |
$capt_fin = [] | |
end | |
if do_action4 =~ /\.\)/ | |
do_action4.gsub!(/\./, '\.') | |
do_action4.gsub!(/\)/, '\)') | |
end | |
if do_action4 =~ /->/ | |
if do_action4.length == 3 | |
do_action4 = " " + do_action4 | |
end | |
end | |
filelines.each do |left_task| | |
if left_task =~ Regexp.new(do_action4) | |
if left_task =~ /\.\)/ | |
left_task.gsub!(/\[_\]/,"[X] \t\t(DONE MOFO!)") | |
puts "BTW, Congratulations for finishing a main task! :D" | |
# fin_tasks += 1 | |
# left_tasks -= 1 | |
# total_tasks = fin_tasks + left_tasks | |
# filelines[-3].gsub!(Regexp.new("Fin:" + (fin_tasks - 1).to_s), "Fin:#{fin_tasks}") | |
# filelines[-3].gsub!(Regexp.new("left:" + (left_tasks - 1).to_s), "#left:{left_tasks}") | |
elsif left_task =~ /->/ | |
left_task.gsub!(/\|\|/, "|X|") | |
puts "BTW, you're one step ahead! :D" | |
end | |
$capt_fin << left_task | |
elsif left_task =~ /Main Tasks/ | |
left_task.gsub!(Regexp.new("Fin: " + (fin_tasks - 1).to_s), "Fin: #{fin_tasks}") | |
left_task.gsub!(Regexp.new("left: " + (left_tasks + 1).to_s), "left: #{left_tasks}") | |
$capt_fin << left_task | |
elsif left_task =~ /Sub Tasks/ | |
left_task.gsub!(Regexp.new("Fin: " + (fin_subtasks - 1).to_s), "Fin: #{fin_subtasks}") | |
left_task.gsub!(Regexp.new("left: " + (left_subtasks + 1).to_s), "left: #{left_subtasks}") | |
$capt_fin << left_task | |
else | |
$capt_fin << left_task | |
end | |
end | |
end | |
break | |
end | |
end | |
break | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment