Class: ArrayAssignmentQuestion
- Inherits:
-
DataStructureAssignmentQuestion
- Object
- Question
- DataStructureQuestion
- DataStructureAssignmentQuestion
- ArrayAssignmentQuestion
- Defined in:
- lib/battleroom/models/array_assignment_question.rb
Constant Summary
Constants inherited from DataStructureAssignmentQuestion
DataStructureAssignmentQuestion::POSSIBLE_INTRO_CONGRATULATIONS
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, #input_mechanism, #user_input, #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_incorrect_input_prompt ⇒ Object
- #print_replace_array_value_prompt ⇒ Object
Methods inherited from DataStructureAssignmentQuestion
#evaluate_data_structure_assignment_input, #print_data_structure, #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
#congratulation_sequence, #enter_evaluation_loop, generate_question, #get_input, #handle_syntax_error_exceptions
Constructor Details
#initialize(scope) ⇒ ArrayAssignmentQuestion
Returns a new instance of ArrayAssignmentQuestion.
8 9 10 11 12 13 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 8 def initialize(scope) super(scope) format_array_for_assignment print_data_structure_assignment_prompt evaluate_data_structure_assignment_input end |
Instance Method Details
#cull_array_to_valid_size_for_output(desired_size) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 27 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
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 80 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 battleprint 'Nope! Try again.'.red end end |
#format_array_for_assignment ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 15 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 |
# 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 print_incorrect_input_prompt end end |
#handles_user_workarounds(user_input) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 67 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 battleprint "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!\n".red else battleprint 'You reassigned the variable '.red + variable_name.green + " rather than working with the array provided. Try again.\n".red end true end end |
#print_data_structure_assignment_prompt ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 40 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 battleprint "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_data_structure end |
#print_incorrect_input_prompt ⇒ Object
60 61 62 63 64 65 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 60 def print_incorrect_input_prompt battleprint "\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 }) battleprint "\nCheck your index and assignment values and try again.\n".red end |
#print_replace_array_value_prompt ⇒ Object
34 35 36 37 38 |
# File 'lib/battleroom/models/array_assignment_question.rb', line 34 def print_replace_array_value_prompt battleprint "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 |