Class: HashAccessQuestion

Inherits:
DataStructureAccessQuestion show all
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

Methods inherited from DataStructureAccessQuestion

#evaluate_data_structure_access_input, #print_data_structure_access_prompt

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) ⇒ HashAccessQuestion

Returns a new instance of HashAccessQuestion.



8
9
10
11
12
13
# File 'lib/battleroom/models/hash_access_question.rb', line 8

def initialize(scope)
  super(scope)
  format_hash
  print_data_structure_access_prompt
  evaluate_data_structure_access_input
end

Instance Method Details

#format_hashObject



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
  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

#remove_multiple_booleansObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/battleroom/models/hash_access_question.rb', line 22

def remove_multiple_booleans
  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