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

Methods inherited from Question

#congratulation_sequence, #enter_evaluation_loop, generate_question, #get_input, #handle_syntax_error_exceptions, #initialize, #post_initialize

Constructor Details

This class inherits a constructor from Question

Instance Method Details

#eval_returns_the_correct_value?(user_input) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/battleroom/models/data_structure_access_question.rb', line 31

def eval_returns_the_correct_value?(user_input)
  evaluation_scope.eval(user_input) == answer_value
end

#evaluate_data_structure_access_inputObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/battleroom/models/data_structure_access_question.rb', line 39

def evaluate_data_structure_access_input
  enter_evaluation_loop do |user_submission|
    begin
      provide_evaluation_scope_with_variable_assignment_necessary_for_answer_eval()
      if eval_returns_the_correct_value?(user_submission) && user_input_uses_variable?(user_submission)
        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

#generate_readable_single_line_hashObject



22
23
24
# File 'lib/battleroom/models/data_structure_access_question.rb', line 22

def generate_readable_single_line_hash
  data_structure.to_s.gsub('{', '{ ').gsub('=>', ' => ').gsub('}', ' }')
end


5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/battleroom/models/data_structure_access_question.rb', line 5

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 < 45
    print_data_structure_on_single_line
  else
    ap(data_structure, { indent: -2, index: false, multiline: true, plain: true })
  end
  battleprint ''
end


26
27
28
29
# File 'lib/battleroom/models/data_structure_access_question.rb', line 26

def print_data_structure_on_single_line
  single_line_hash_as_string = generate_readable_single_line_hash()
  battleprint(single_line_hash_as_string)
end

#provide_evaluation_scope_with_variable_assignment_necessary_for_answer_evalObject



18
19
20
# File 'lib/battleroom/models/data_structure_access_question.rb', line 18

def provide_evaluation_scope_with_variable_assignment_necessary_for_answer_eval
  evaluation_scope.eval("#{variable_name} = #{data_structure.to_s}")
end

#user_input_uses_variable?(user_input) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/battleroom/models/data_structure_access_question.rb', line 35

def user_input_uses_variable?(user_input)
  user_input.include?(variable_name)
end