Class: HashAssignmentQuestion

Inherits:
DataStructureAssignmentQuestion show all
Defined in:
lib/battleroom/models/hash_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

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, #initialize

Constructor Details

This class inherits a constructor from Question

Instance Method Details

#evaluate_user_input(user_input) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/battleroom/models/hash_assignment_question.rb', line 29

def evaluate_user_input(user_input)
  if evaluation_scope.eval("#{variable_name}[#{assignment_key}] == #{formatted_assignment_value}") && user_input.include?(variable_name)
    print_resulting_data_structure
    true
  else
    battleprint "Nope! Try again!".red
  end
end

#format_hash_for_assignmentObject



15
16
17
18
19
20
21
22
# File 'lib/battleroom/models/hash_assignment_question.rb', line 15

def format_hash_for_assignment
  cull_hash_to_valid_size_for_output
  assignment                      = possible_assignments.sample
  self.assignment_value           = assignment.values[0]
  self.assignment_value_class     = format_class_for_output(assignment_value.class)
  self.formatted_assignment_value = format_value_for_stdout_and_eval(assignment_value)
  self.assignment_key             = format_value_for_stdout_and_eval(assignment.keys[0])
end

#post_initializeObject



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

def post_initialize
  super
  format_hash_for_assignment
  print_data_structure_assignment_prompt
  evaluate_data_structure_assignment_input
end


24
25
26
27
# File 'lib/battleroom/models/hash_assignment_question.rb', line 24

def print_data_structure_assignment_prompt
  battleprint 'Given the Hash below, add a key of '.blue + assignment_key.yellow + " that points to the #{assignment_value_class} value of ".blue + "#{formatted_assignment_value}".yellow + ".\n\n"
  print_data_structure
end