Class: VariableAssignmentQuestion

Inherits:
Question
  • Object
show all
Defined in:
lib/battleroom/models/variable_assignment_question.rb

Instance Attribute Summary collapse

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 Question

#congratulation_sequence, #enter_evaluation_loop, generate_question, #get_input, #handle_syntax_error_exceptions

Constructor Details

#initialize(eval_scope) ⇒ VariableAssignmentQuestion

Returns a new instance of VariableAssignmentQuestion.



9
10
11
12
13
14
15
# File 'lib/battleroom/models/variable_assignment_question.rb', line 9

def initialize(eval_scope)
  super(eval_scope)
  @variable_value = rotate_array(data[:possible_variable_values]).first
  @formatted_value = format_value_for_stdout_and_eval(variable_value)
  print_variable_assignment_prompt
  evaluate_variable_assignment_input
end

Instance Attribute Details

#formatted_classObject

Returns the value of attribute formatted_class.



6
7
8
# File 'lib/battleroom/models/variable_assignment_question.rb', line 6

def formatted_class
  @formatted_class
end

#formatted_valueObject

Returns the value of attribute formatted_value.



6
7
8
# File 'lib/battleroom/models/variable_assignment_question.rb', line 6

def formatted_value
  @formatted_value
end

Instance Method Details

#evaluate_variable_assignment_inputObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/battleroom/models/variable_assignment_question.rb', line 59

def evaluate_variable_assignment_input
  enter_evaluation_loop do |user_submission|
    begin
      evaluation_scope.eval(user_submission)
      if evaluation_scope.eval("#{variable_name} == #{formatted_value}")
        true
      else
        battleprint "You assigned the wrong value to #{variable_name}. Try again!".red
      end
    rescue NameError => e
      reveal_name_error_follies_to_user(user_submission, e)
    rescue Exception => e
      handle_base_exception(e)
    end
  end
end

#handle_base_exception(exception) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/battleroom/models/variable_assignment_question.rb', line 51

def handle_base_exception(exception)
  if exception.message.match(/unterminated string/)
    battleprint 'Blurg! You neglected to provide closing quotes for your string. Try again!'.red
  else
    battleprint exception.message
  end
end


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/battleroom/models/variable_assignment_question.rb', line 17

def print_variable_assignment_prompt
  self.formatted_class = format_class_for_output(variable_value.class)
  substrings = [
    'Assign the variable '.blue,
    variable_name.yellow,
    ' to the '.blue,
    formatted_class.blue,
    ' value '.blue,
    formatted_value.yellow,
    ".\n".blue
  ]
  battleprint substrings.join
end

#reveal_name_error_follies_to_user(user_input, errorbefor) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/battleroom/models/variable_assignment_question.rb', line 31

def reveal_name_error_follies_to_user(user_input, errorbefor)
  if user_input.include?(variable_name)
    if !user_input.match(/[^=]=[^=]/i)
      undefined_local_variable_prompt_for_correct_variable
    else
      misassignment_prompt
    end
  elsif !user_input.match(/("|')/) && variable_value.class == String
    battleprint 'Rats! You\'ve just made a common rookie mistake! Strings are always surrounded by quotes. Otherwise, Ruby will think you\'re referencing a variable or method name. Try again.'.red
  else
    battleprint "Looks like you mistyped the variable name. Check for misspellings and try again.".red
  end
end

#undefined_local_variable_prompt_for_correct_variableObject



45
46
47
48
49
# File 'lib/battleroom/models/variable_assignment_question.rb', line 45

def undefined_local_variable_prompt_for_correct_variable
  battleprint "\nYou're referencing a variable that hasn't been assigned yet. This results in a common error that says: \n".red
  battleprint "\tundefined local variable or method \'#{variable_name}\'\n".green
  battleprint "To assign a value to a variable, you'll need to use the assignment operator ".red + ' = '.black.on_light_white + ", followed by a valid Ruby value.\n".red
end