Class: VariableReferenceQuestion

Inherits:
FollowUpQuestion show all
Defined in:
lib/battleroom/models/variable_reference_question.rb

Instance Attribute Summary collapse

Attributes inherited from FollowUpQuestion

#original_question

Attributes inherited from Question

#answer_value, #data, #data_structure, #data_structure_class, #evaluation_scope, #explanation, #input_mechanism, #user_input

Instance Method Summary collapse

Methods inherited from FollowUpQuestion

#initialize

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 FollowUpQuestion

Instance Attribute Details

#formatted_variable_classObject (readonly)

Returns the value of attribute formatted_variable_class.



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

def formatted_variable_class
  @formatted_variable_class
end

#formatted_variable_valueObject (readonly)

Returns the value of attribute formatted_variable_value.



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

def formatted_variable_value
  @formatted_variable_value
end

#required_classObject

Returns the value of attribute required_class.



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

def required_class
  @required_class
end

#required_return_valueObject

Returns the value of attribute required_return_value.



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

def required_return_value
  @required_return_value
end

#variable_nameObject (readonly)

Returns the value of attribute variable_name.



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

def variable_name
  @variable_name
end

#variable_valueObject (readonly)

Returns the value of attribute variable_value.



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

def variable_value
  @variable_value
end

Instance Method Details

#a_value_that_doesnt_result_from_imprecise_float_arithmeticObject



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

def a_value_that_doesnt_result_from_imprecise_float_arithmetic
  if variable_value < 15
    new_target_value = variable_value + rand(1..15)
  else
    new_target_value = a_value_within_12_more_or_12_less_than_the_original_assigned_value
  end
  count = 1
  while new_target_value.to_s.split(".").last.length > 4
    new_target_value = variable_value + count
    count += 1
  end
  new_target_value
end

#a_value_that_requires_low_arithmetic_complexity_to_arrive_at_via_multiplication_or_divisionObject



45
46
47
48
49
50
51
52
53
# File 'lib/battleroom/models/variable_reference_question.rb', line 45

def a_value_that_requires_low_arithmetic_complexity_to_arrive_at_via_multiplication_or_division
  if variable_value < 12
    variable_value * [3,4,5,6].sample
  elsif variable_value < 50
    variable_value * 2
  else
    variable_value / 2
  end
end

#a_value_within_12_more_or_12_less_than_the_original_assigned_valueObject



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

def a_value_within_12_more_or_12_less_than_the_original_assigned_value
  range = (-12..12).to_a
  range.delete(0)
  variable_value + range.sample
end

#coach_user_and_reprompt(return_value_of_user_input) ⇒ Object



82
83
84
# File 'lib/battleroom/models/variable_reference_question.rb', line 82

def coach_user_and_reprompt(return_value_of_user_input)
  puts "Your code returned the #{format_class_for_output(return_value_of_user_input.class)} value #{return_value_of_user_input.to_s} when it should have returned the #{formatted_variable_class} value #{format_value_for_stdout_and_eval(required_return_value)}. Try again.".red
end

#develop_promptObject



67
68
69
70
71
72
# File 'lib/battleroom/models/variable_reference_question.rb', line 67

def develop_prompt
  self.required_return_value = generate_appropriate_value()
  required_return_value_colorized = required_return_value.to_s.green
  self.required_class = original_question.formatted_class
  "Use ".blue + variable_name.green + " in combination with an arithmetic operator like ".blue + colorized_arithmetic_operator_list + " to return the #{required_class} value ".blue + required_return_value_colorized + ".\n".blue
end

#evaluate_variable_reference_inputObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/battleroom/models/variable_reference_question.rb', line 86

def evaluate_variable_reference_input
  enter_evaluation_loop do |user_submission|
    begin
      provide_evaluation_scope_with_original_variable_assignment
      returned_value = evaluation_scope.eval(user_submission)
      if user_doesnt_use_variable?(user_submission)
        battleprint "You didn't make use of the '#{variable_name}' variable, which is the entire purpose of this exercise. Try again.".red
      elsif user_submission.include?("=")
        battleprint "Looks like you simply assigned (or reassigned) a value to a variable, rather than making use of the value stored in '#{variable_name}'. Reread the directions and try again!".red
      elsif (returned_value == required_return_value)
        true
      else
        coach_user_and_reprompt(returned_value)
      end
    rescue NameError, NoMethodError => e
      print_colorized_error_prompt(e)
    end
  end
end

#generate_appropriate_valueObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/battleroom/models/variable_reference_question.rb', line 55

def generate_appropriate_value
  if variable_value.class == Fixnum
    if variable_value.even?
      a_value_that_requires_low_arithmetic_complexity_to_arrive_at_via_multiplication_or_division()
    else
      a_value_within_12_more_or_12_less_than_the_original_assigned_value()
    end
  else
    a_value_that_doesnt_result_from_imprecise_float_arithmetic()
  end
end

#post_initializeObject



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

def post_initialize
  @variable_name            = original_question.variable_name
  @variable_value           = original_question.variable_value
  @formatted_variable_value = original_question.formatted_value
  @formatted_variable_class = original_question.formatted_class
  print_variable_reference_prompt
  evaluate_variable_reference_input
end


18
19
20
21
22
23
# File 'lib/battleroom/models/variable_reference_question.rb', line 18

def print_variable_reference_prompt
  battleprint "You now have the variable assignment below at your disposal.\n".blue
  battleprint "\t#{variable_name} = #{formatted_variable_value}\n\n"
  prompt = develop_prompt()
  puts prompt
end

#provide_evaluation_scope_with_original_variable_assignmentObject



74
75
76
# File 'lib/battleroom/models/variable_reference_question.rb', line 74

def provide_evaluation_scope_with_original_variable_assignment
  evaluation_scope.eval("#{variable_name} = #{formatted_variable_value}")
end

#user_doesnt_use_variable?(user_input) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/battleroom/models/variable_reference_question.rb', line 78

def user_doesnt_use_variable?(user_input)
  !user_input.include?(variable_name)
end