Class: DataStructureAccessQuestion

Inherits:
DataStructureQuestion show all
Defined in:
lib/battleroom/models/data_structure_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 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(eval_scope) ⇒ DataStructureAccessQuestion

Returns a new instance of DataStructureAccessQuestion.



5
6
7
# File 'lib/battleroom/models/data_structure_access_question.rb', line 5

def initialize(eval_scope)
  super(eval_scope)
end

Instance Method Details

#evaluate_data_structure_access_inputObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/battleroom/models/data_structure_access_question.rb', line 22

def evaluate_data_structure_access_input
  enter_evaluation_loop do |user_submission|
    begin
      # provides the evaluation scope with variable assignment necessary for answer eval
      evaluation_scope.eval("#{variable_name} = #{data_structure.to_s}")
      if evaluation_scope.eval(user_submission) == answer_value && user_submission.include?(variable_name)
        true
      else
        battleprint "Remember, #{hint} Try again.".red
      end
    rescue NameError => e
      print_colorized_error_prompt(e)
    rescue TypeError => e
      print_colorized_type_error_prompt(e)
    end
  end
end


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/battleroom/models/data_structure_access_question.rb', line 9

def print_data_structure_access_prompt
  answer_value_class = format_class_for_output(answer_value.class)
  answer_value_string = format_value_for_stdout_and_eval(answer_value)
  battleprint "Given the data structure below, access the #{answer_value_class} value ".blue + answer_value_string.yellow + ".\n".blue
  print "#{variable_name} = ".green
  if data_structure.to_s.length < 40
    battleprint data_structure.to_s.gsub('{', '{ ').gsub('=>', ' => ').gsub('}', ' }')
  else
    ap(data_structure, { indent: -2, index: false, multiline: true, plain: true })
  end
  battleprint ''
end