Class: ArrayAssignmentQuestion
- Inherits:
-
DataStructureAssignmentQuestion
- Object
- Question
- DataStructureQuestion
- DataStructureAssignmentQuestion
- ArrayAssignmentQuestion
- Defined in:
- lib/battleroom/models/array_assignment_question.rb
Instance Attribute Summary
Attributes inherited from DataStructureAssignmentQuestion
#assignment_key, #assignment_value, #assignment_value_class, #formatted_assignment_value, #replacement_index, #replacement_value_class_formatted, #value_to_replace, #value_to_replace_formatted
Attributes inherited from DataStructureQuestion
#data_structure, #hint, #possible_assignments
Attributes inherited from Question
#answer_value, #data, #data_structure, #data_structure_class, #evaluation_scope, #explanation, #type, #variable_name, #variable_value
Instance Method Summary collapse
- #cull_array_to_valid_size_for_output(desired_size) ⇒ Object
- #evaluate_user_input(user_input) ⇒ Object
- #format_array_for_assignment ⇒ Object
- #handle_replacement_of_array_value_input(user_input) ⇒ Object
- #handles_user_workarounds(user_input) ⇒ Object
-
#initialize(scope) ⇒ ArrayAssignmentQuestion
constructor
A new instance of ArrayAssignmentQuestion.
- #print_data_structure_assignment_prompt ⇒ Object
- #print_replace_array_value_prompt ⇒ Object
Methods inherited from DataStructureAssignmentQuestion
#evaluate_data_structure_assignment_input, #print_resulting_data_structure
Methods inherited from DataStructureQuestion
#convert_keys_to_strings, #cull_hash_to_valid_size_for_output, #find_number_of_boolean_values_in_hash
Methods inherited from Question
#enter_evaluation_loop, generate_question
Constructor Details
#initialize(scope) ⇒ ArrayAssignmentQuestion
Returns a new instance of ArrayAssignmentQuestion.
8 9 10 11 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 8 def initialize(scope) super(scope) format_array_for_assignment end |
Instance Method Details
#cull_array_to_valid_size_for_output(desired_size) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 25 def cull_array_to_valid_size_for_output(desired_size) while data_structure.size > desired_size new_assignment_possibility = data_structure.delete(data_structure.sample) possible_assignments.push(new_assignment_possibility) end end |
#evaluate_user_input(user_input) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 77 def evaluate_user_input(user_input) if handles_user_workarounds(user_input) false end if value_to_replace handle_replacement_of_array_value_input(user_input) elsif evaluation_scope.eval("#{variable_name}.last == #{formatted_assignment_value}") && user_input.include?(variable_name) print_resulting_data_structure true else puts 'Nope! Try again.'.red end end |
#format_array_for_assignment ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 13 def format_array_for_assignment desired_array_size = rand(3..6) cull_array_to_valid_size_for_output(desired_array_size) if [1, 2, 3, 4, 5].sample.odd? self.value_to_replace = data_structure.sample self.value_to_replace_formatted = format_value_for_stdout_and_eval(value_to_replace) end self.assignment_value = possible_assignments.sample self.assignment_value_class = format_class_for_output(assignment_value.class) self.formatted_assignment_value = format_value_for_stdout_and_eval(assignment_value) end |
#handle_replacement_of_array_value_input(user_input) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 51 def handle_replacement_of_array_value_input(user_input) if evaluation_scope.eval("#{variable_name}[#{replacement_index}] == #{formatted_assignment_value}") && user_input.include?(variable_name) print_resulting_data_structure true else puts "\nNope! Here's what the data stucture would look like given your code:\n".red resulting_data_structure = evaluation_scope.eval(variable_name) ap(resulting_data_structure, { indent: -2, index: false, multiline: true, plain: true }) puts "\nCheck your index and assignment values and try again.\n".red end end |
#handles_user_workarounds(user_input) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 63 def handles_user_workarounds(user_input) cheater_regex = Regexp.new("#{variable_name}\s+?\=\s+?(\\[)?") # checks if user reassigned the variable to a new array of identical values if user_input.match(cheater_regex) if $1 puts 'You reassigned the variable to a new array object, when you could have worked with the array provided! Look up Ruby\'s Array#push method and try again!'.red else puts 'You reassigned the variable '.red + variable_name.green + ' rather than working with the array provided. Try again.'.red end puts '' true end end |
#print_data_structure_assignment_prompt ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 38 def print_data_structure_assignment_prompt if value_to_replace self.replacement_index = data_structure.index(value_to_replace) self.replacement_value_class_formatted = format_class_for_output(value_to_replace.class) print_replace_array_value_prompt else puts "Use an array method to add the #{assignment_value_class} value ".blue + "#{formatted_assignment_value}".yellow + " to the ".blue + "end".blue.underline + " of the Array below.\n".blue end print "#{variable_name} = ".green ap(data_structure, { indent: -2, index: false, multiline: true, plain: true }) puts '' end |
#print_replace_array_value_prompt ⇒ Object
32 33 34 35 36 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 32 def print_replace_array_value_prompt puts "Given the data structure below, replace the #{replacement_value_class_formatted} value ".blue + value_to_replace_formatted.yellow + " with the #{assignment_value_class} value ".blue + formatted_assignment_value.yellow + ".\n\n" end |