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, #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(scope, original_question) ⇒ VariableReferenceQuestion

Returns a new instance of VariableReferenceQuestion.



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

def initialize(scope, original_question)
  super(scope, original_question)
  print_variable_reference_prompt
  evaluate_variable_reference_input
end

Instance Attribute Details

#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

Instance Method Details

#a_value_that_doesnt_result_from_imprecise_float_arithmeticObject



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

def a_value_that_doesnt_result_from_imprecise_float_arithmetic
  if original_question.variable_value < 15
    new_target_value = original_question.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 = original_question.variable_value + count
    count += 1
  end
  new_target_value
end

#a_value_within_12_more_or_12_less_than_the_original_assigned_valueObject



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

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

#develop_promptObject



59
60
61
62
63
64
65
66
67
# File 'lib/battleroom/models/variable_reference_question.rb', line 59

def develop_prompt
  case original_question.variable_value.class.to_s
  when 'Fixnum', 'Float'
   self.required_return_value = generate_appropriate_value()
   prompt = "Use ".blue + original_question.variable_name.green + " in combination with an arithmetic operator like ".blue +  " + ".black.on_light_white + ", ".blue + " - ".black.on_light_white + ", ".blue + " * ".black.on_light_white + ", or ".blue + " / ".black.on_light_white + " to return the #{original_question.formatted_class} value ".blue + required_return_value.to_s.green + ".\n".blue
  else
    battleprint "Something's gone wrong. This code should never run.".red
  end
end

#evaluate_variable_reference_inputObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/battleroom/models/variable_reference_question.rb', line 73

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_submission.include?(original_question.variable_name)
        battleprint "You didn't make use of the '#{original_question.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 '#{original_question.variable_name}'. Reread the directions and try again!".red
      elsif (returned_value == required_return_value)
        true
      else
        puts "Your code returned the #{format_class_for_output(returned_value.class)} value #{returned_value.to_s} when it should have returned the #{original_question.formatted_class} value #{format_value_for_stdout_and_eval(required_return_value)}. Try again.".red
      end
    rescue NameError, NoMethodError => e
      print_colorized_error_prompt(e)
    end
  end
end

#generate_appropriate_valueObject



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

def generate_appropriate_value
  value = original_question.variable_value
  if value.class == Fixnum
    if value.even? # && [1,2,3,4,5].sample.even?
      if value < 12
        value * [3,4,5,6].sample
      elsif value < 50
        value * 2
      else
        value / 2
      end
    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


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

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

#provide_evaluation_scope_with_original_variable_assignmentObject



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

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