Class: HashAssignmentQuestion

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

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

Returns a new instance of HashAssignmentQuestion.



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

def initialize(scope)
  super(scope)
  format_hash_for_assignment
end

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
    puts 'Nope! Try again!'.red
  end
end

#format_hash_for_assignmentObject



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

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


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

def print_data_structure_assignment_prompt
  puts '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 "#{variable_name} = ".green
  ap(data_structure, { indent: -2, index: false, multiline: true, plain: true })
  puts ''
end