Skip to content

Instantly share code, notes, and snippets.

@rskelley9
Last active July 6, 2018 04:51
Show Gist options
  • Save rskelley9/4beb11f0cf0a4c31abfc55d14ed1926b to your computer and use it in GitHub Desktop.
Save rskelley9/4beb11f0cf0a4c31abfc55d14ed1926b to your computer and use it in GitHub Desktop.
Just for fun because I never write recursive methods.
DEFAULT_TIMES_TO_RUN = 1
def build_msg(types_data:{words: [], numbers: []})
msg = "these are the words: #{types_data[:words]}, these are the numbers: #{types_data[:numbers]}"
if types_data[:other] and types_data[:other].class.eql(Array) and types_data[:other].length > 0
msg += ", these are the other elements: #{types_data[:other]}"
end
msg
end
def abort_or_exit?(user_entry)
!(user_entry =~ /^abort$/ix).nil? || (user_entry =~ /^exit$/ix).nil?
end
def list_types(list:, types: {words: [], numbers: []})
return puts build_msg(types_data: types) if list.length == 0
if list[0] =~ /\d/
types[:numbers] << list[0].to_i
elsif list[0] =~ /\w/
types[:words] << list[0]
else
if types[:other].nil?
types[:other] = []
end
types[:other] << list[0]
end
list_types(list: (list = list[1..-1]), types: types)
end
def user_greet
puts "Hello, this script will parse any list by class type."
puts "To get started, enter the number of lists you'd like to examine... \n"
end
def user_prompt_list
puts "Next, please enter list of characters delineated by ',''"
puts "Note, you may terminate this program at any time by entering 'ABORT' or 'EXIT'"
end
def get_times_to_run
user_entry = gets.chomp
if user_entry.to_s.length == 0
raise ArgumentError, "Error: did not specify number of times to run script!"
elsif (user_entry =~ /\d/).nil?
raise ArgumentError, "Error: unrecognized argument! #{user_entry}."
end
user_entry.to_i
end
def parse_list!(run_count:, times_to_run:)
begin
user_prompt_list
until run_count == times_to_run
puts "\n\n enter list..."
list = gets.chomp.to_s.delete(' ').split(",")
if list.length < 1
raise ArgumentError, 'Error: Argument is not a list'
elsif list.length == 1
abort("ABORTED") if abort_or_exit?(list[0])
elsif times_to_run >= 100_000
raise ArgumentError, 'Error: Are you insane?'
end
list_types(list: list)
run_count +=1
end
end
end
def run!
user_greet
parse_list!(run_count: 0, times_to_run: get_times_to_run)
"DONE!"
end
run!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment