Class: HashAccessQuestion
- Inherits:
-
DataStructureAccessQuestion
- Object
- Question
- DataStructureQuestion
- DataStructureAccessQuestion
- HashAccessQuestion
- Defined in:
- lib/battleroom/models/hash_access_question.rb
Instance Attribute Summary
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
- #format_hash ⇒ Object
- #post_initialize ⇒ Object
- #remove_multiple_booleans_to_avoid_possibility_of_ambiguous_prompt ⇒ Object
Methods inherited from DataStructureAccessQuestion
#eval_returns_the_correct_value?, #evaluate_data_structure_access_input, #generate_readable_single_line_hash, #print_data_structure_access_prompt, #print_data_structure_on_single_line, #provide_evaluation_scope_with_variable_assignment_necessary_for_answer_eval, #user_input_uses_variable?
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, #initialize
Constructor Details
This class inherits a constructor from Question
Instance Method Details
#format_hash ⇒ Object
15 16 17 18 19 20 |
# File 'lib/battleroom/models/hash_access_question.rb', line 15 def format_hash cull_hash_to_valid_size_for_output remove_multiple_booleans_to_avoid_possibility_of_ambiguous_prompt self.answer_value = data_structure[data_structure.keys.sample] self.hint = 'you have to use the EXACT hash key to retrieve the associated value.' end |
#post_initialize ⇒ Object
8 9 10 11 12 13 |
# File 'lib/battleroom/models/hash_access_question.rb', line 8 def post_initialize super format_hash print_data_structure_access_prompt evaluate_data_structure_access_input end |
#remove_multiple_booleans_to_avoid_possibility_of_ambiguous_prompt ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/battleroom/models/hash_access_question.rb', line 22 def remove_multiple_booleans_to_avoid_possibility_of_ambiguous_prompt boolean_count = find_number_of_boolean_values_in_hash while boolean_count > 1 grouped_by_value = data_structure.group_by { |k, v| v } boolean_to_delete = [true, false].sample key_to_delete = grouped_by_value[boolean_to_delete].sample[0] if grouped_by_value[boolean_to_delete] data_structure.delete(key_to_delete) boolean_count = find_number_of_boolean_values_in_hash end end |