battleroom 0.0.87 → 0.0.871

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/lib/battleroom.rb +5 -5
  3. data/lib/battleroom/battleroom_machinery.rb +5 -111
  4. data/lib/battleroom/config/boot.rb +1 -0
  5. data/lib/battleroom/config/pry_config.rb +9 -11
  6. data/lib/battleroom/data/array_questions.rb +1 -1
  7. data/lib/battleroom/data/hash_questions.rb +1 -1
  8. data/lib/battleroom/data/variable_assignment_questions.rb +1 -1
  9. data/lib/battleroom/{data_generation_machinery.rb → helpers/data_generation_machinery.rb} +0 -0
  10. data/lib/battleroom/helpers/exceptionable.rb +36 -0
  11. data/lib/battleroom/helpers/printable.rb +82 -0
  12. data/lib/battleroom/models/array_access_question.rb +2 -2
  13. data/lib/battleroom/models/array_assignment_question.rb +2 -2
  14. data/lib/battleroom/models/data_structure_access_question.rb +25 -9
  15. data/lib/battleroom/models/data_structure_assignment_question.rb +2 -2
  16. data/lib/battleroom/models/data_structure_question.rb +1 -2
  17. data/lib/battleroom/models/follow_up_question.rb +5 -0
  18. data/lib/battleroom/models/hash_access_question.rb +4 -4
  19. data/lib/battleroom/models/hash_assignment_question.rb +6 -6
  20. data/lib/battleroom/models/method_definition_question.rb +84 -85
  21. data/lib/battleroom/models/method_invocation_question.rb +74 -75
  22. data/lib/battleroom/models/nested_data_structure_access_question.rb +1 -2
  23. data/lib/battleroom/models/question.rb +6 -2
  24. data/lib/battleroom/models/variable_assignment_question.rb +1 -2
  25. data/lib/battleroom/models/variable_reassignment_question.rb +10 -7
  26. data/lib/battleroom/models/variable_reference_question.rb +45 -32
  27. metadata +19 -5
  28. data/lib/battleroom/helpers/array_formatter.rb +0 -3
  29. data/lib/battleroom/helpers/hash_formatter.rb +0 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea6eacbcacf28f4bca5940747bf9f4da36ef5d13
4
- data.tar.gz: 21b8d9c2b78ab1a42fef3cc600288fe854213c3f
3
+ metadata.gz: 409f7278a020fb96436c88637a9e4b66fe6a5474
4
+ data.tar.gz: 734eb52c7e3e8f4ad0b45d7d83d26c06e5136ae9
5
5
  SHA512:
6
- metadata.gz: 3eb408861d7139e022230ccda9065e86f532a826c6d23b3d7f1b8941ac1e6c407f61b5b5b5b37069c6259706a793d124908c3187166ae80e510aa8f0de53f65f
7
- data.tar.gz: 566149a7846b4534eb531a3480c95575a89e294319e2316c67681837d5ed555cc385690adce577519400bb121abac2c3ea1fb195acd69300ff090309cb3672b4
6
+ metadata.gz: 4544cad6b48559e668db2fff4d1c56875f804090ec706c7cc9f911d4f930278fd6848119e4ee371babe6f884c5a886a8acadfadef9b9c10aa5a2cf188c422d99
7
+ data.tar.gz: 2bd6d79c5a80621e26b54ea7d61f9a8b64c2a66b87a12db7a1396f50d085fa1e543483f7eedde0c68755cf3fb8f3f4eeeaa0a343cb3eba26f80977a093ce6d78
@@ -28,11 +28,11 @@ loop do
28
28
  5.times do
29
29
  q = [ArrayAssignmentQuestion.new(b), HashAssignmentQuestion.new(b)].sample
30
30
  end
31
- # when '4'
32
- # 5.times do
33
- # q = MethodDefinitionQuestion.new(b)
34
- # follow_up_question = MethodInvocationQuestion.new(b, q)
35
- # end
31
+ when '4'
32
+ 5.times do
33
+ q = MethodDefinitionQuestion.new(b)
34
+ follow_up_question = MethodInvocationQuestion.new(b, q)
35
+ end
36
36
  when '4'
37
37
  5.times { NestedDataStructureAccessQuestion.new(b) }
38
38
  else
@@ -1,96 +1,19 @@
1
- module BattleroomMachinery
1
+ require_relative 'helpers/printable'
2
+ require_relative 'helpers/exceptionable'
2
3
 
3
- CONGRATULATIONS = [
4
- 'Lovely work, my friend!',
5
- 'Beautiful!',
6
- 'Lovely!',
7
- 'Splendid!',
8
- 'Awesome.',
9
- 'Stand up job, my friend.',
10
- 'Nailed it.',
11
- 'Nice job! Keep it rolling!',
12
- 'Capital work, battlestar!',
13
- 'Exemplary work!',
14
- 'Yeah!',
15
- 'Roll on.',
16
- 'Bullseye!',
17
- 'Woo!',
18
- 'Let it ride!',
19
- 'Touchdown!',
20
- "You're on your way!",
21
- "You're making a prosperous go of this programming thing.",
22
- ]
4
+ module BattleroomMachinery
5
+ include Battleroom::Printable
6
+ include Battleroom::Exceptionable
23
7
 
24
8
  def clear_display
25
9
  `reset`
26
10
  end
27
11
 
28
- def print_menu_options
29
- battleprint ' What would you like to work on?'.blue
30
- battleprint '1. Working with Variables'
31
- battleprint '2. Accessing Values in Arrays and Hashes'
32
- battleprint '3. Adding Values to Arrays and Hashes'
33
- # battleprint '4. Working with Methods'
34
- battleprint "Q. Quit\r\n\n"
35
- end
36
-
37
- def random_congratulation
38
- CONGRATULATIONS.sample
39
- end
40
-
41
-
42
- def print_congratulation
43
- battleprint "\n#{random_congratulation}\n".green
44
- end
45
-
46
12
  def rotate_array(array)
47
13
  item = array.shift
48
14
  array << item
49
15
  end
50
16
 
51
- def print_colorized_error_prompt(error)
52
- method_or_variable = (error.class == NoMethodError) ? "method" : "variable"
53
- battleprint "\nYou're referencing a #{method_or_variable} that doesn't exist, probably as the result of a mispelling. This results in a common Ruby error that reads: \n".red
54
- battleprint "\tundefined local variable or method \'WHATEVER_YOU_MISTYPED\'\n".green
55
- battleprint "Get used to it and try again.\n".red
56
- end
57
-
58
- def indent_all_lines_for_stdout(string_to_indent)
59
- string_to_indent.gsub(/^.*/) { |match| "\t" + match }
60
- end
61
-
62
- def dynamic_word_wrap(string)
63
- string.wrap(150)
64
- end
65
-
66
- def battleprint(string)
67
- puts dynamic_word_wrap(string)
68
- end
69
-
70
- def isolate_variable_name_from_name_error(error)
71
- /`(.+)'/i.match(error.message)
72
- end
73
-
74
- def window_width
75
- begin
76
- width_string = `tput cols`
77
- # for cases where systems don't respond to the tput command
78
- rescue StandardError => e
79
- width_string = "80"
80
- end
81
- width_string.strip.to_i
82
- end
83
-
84
- def print_unexpected_end_of_input_explanation(error)
85
- battleprint "\nNope! You just triggered a common Ruby error that reads:\n".red
86
- battleprint "\tsyntax error, unexpected end-of-input\n".green
87
- if error.message.include?(']')
88
- battleprint "Basically, you used an opening square bracket '[', but didn't pair it with a closing square bracket. Try again.\n".red
89
- else
90
- battleprint "Basically, you told Ruby you were going to assign a value to a variable, but you neglected to provide a valid value. Try again.\n".red
91
- end
92
- end
93
-
94
17
  def determine_variable_follow_up_question(eval_scope, question)
95
18
  if question.variable_value == true || question.variable_value == false
96
19
  VariableReassignmentQuestion.new(eval_scope, question)
@@ -101,30 +24,6 @@ module BattleroomMachinery
101
24
  end
102
25
  end
103
26
 
104
- def print_colorized_type_error_prompt(error)
105
- battleprint "\nNope! You just triggered a common Ruby error that reads:\n".red
106
- battleprint "\tin '[]', #{error.message}".green
107
- error.message.match /conversion\sof\s(.+)\sinto\sInteger/i
108
- battleprint "\nBasically, you put a #{$1} between square brackets, whereas Ruby ".red +
109
- "was expecting an index value, i.e. an integer. This commonly arises ".red +
110
- "when programmers think they're dealing with a hash, when in fact ".red +
111
- "they're dealing with an array. Try again.\n".red
112
- end
113
-
114
- def format_value_for_stdout_and_eval(object)
115
- case object.class.to_s
116
- when 'String'
117
- if object.include?("'")
118
- '"' + object + '"'
119
- else
120
- "'#{object}'"
121
- end
122
- when 'Symbol' then return ":#{object}"
123
- else
124
- object.to_s
125
- end
126
- end
127
-
128
27
  def naughty_input?(user_input)
129
28
  if user_input.match(/(require|`|binding)/)
130
29
  battleprint "No way no how! Try again.\n".red
@@ -134,9 +33,4 @@ module BattleroomMachinery
134
33
  end
135
34
  end
136
35
 
137
- def format_class_for_output(klass)
138
- return 'Boolean' if klass.to_s.match /(TrueClass|FalseClass)/
139
- klass.to_s
140
- end
141
-
142
36
  end
@@ -5,6 +5,7 @@ require 'pry'
5
5
  require 'colorize'
6
6
  require 'coderay'
7
7
  require 'word_wrap'
8
+ require 'rubygems/dependency_installer'
8
9
 
9
10
  path = File.expand_path("../models/*.rb", File.dirname(__FILE__))
10
11
  Dir[path].each { |file| require file }
@@ -3,17 +3,14 @@ I18n.config.enforce_available_locales = false
3
3
 
4
4
  def configure_pry
5
5
  Pry.config.default_window_size = 0
6
- Pry.config.quiet = true
7
- Pry.prompt = [
8
- proc { "> ".blue },
9
- proc { "* ".blue }
10
- ]
11
- Pry.config.memory_size = 10
6
+ Pry.config.quiet = true
7
+ Pry.config.memory_size = 10
8
+ Pry.prompt = [proc { "> ".blue }, proc { "* ".blue }]
12
9
 
13
10
  # removes pry's exit command, which is run before any of the before_eval hooks, resulting in a pry session users can't escape
14
11
  Pry::Commands.delete("exit")
15
12
 
16
- # # quiets pry error caused by self_termination (below)
13
+ # quiets pry error caused by self_termination (below)
17
14
  Pry.config.exception_handler = proc { |output, exception, _| }
18
15
 
19
16
  # short circuits pry REPL before eval, saving eval for Question
@@ -21,7 +18,7 @@ def configure_pry
21
18
  begin
22
19
  # exports user input for availability
23
20
  $input = last_input
24
- unless last_input.include?("revert_pry_to_defaults")
21
+ unless last_input.include?("restore_pry_defaults")
25
22
  pry_instance.run_command("continue")
26
23
  end
27
24
  # quiets ArgumentError thrown by pry-byebug as a result of self_termination hook (above)
@@ -30,11 +27,12 @@ def configure_pry
30
27
  end
31
28
  end
32
29
 
30
+ # for use in debugging
33
31
  def restore_pry_defaults
34
32
  Pry.config.default_window_size = 15
35
- Pry.config.quiet = false
36
- Pry.prompt = Pry::DEFAULT_PROMPT
37
- Pry.config.hooks.delete_hook :before_eval, :self_terminate
33
+ Pry.config.quiet = false
34
+ Pry.prompt = Pry::DEFAULT_PROMPT
35
+ Pry.config.hooks.delete_hook :before_eval, :self_terminate
38
36
  end
39
37
 
40
38
  configure_pry
@@ -1,4 +1,4 @@
1
- require_relative '../data_generation_machinery'
1
+ require_relative '../helpers/data_generation_machinery'
2
2
  include DataGenerationMachinery
3
3
 
4
4
  $random_names_array = gen_random_names_array
@@ -1,4 +1,4 @@
1
- require_relative '../data_generation_machinery'
1
+ require_relative '../helpers/data_generation_machinery'
2
2
  include DataGenerationMachinery
3
3
  require 'active_support/inflector'
4
4
 
@@ -1,4 +1,4 @@
1
- require_relative '../data_generation_machinery'
1
+ require_relative '../helpers/data_generation_machinery'
2
2
  include DataGenerationMachinery
3
3
 
4
4
  $random_names_array = gen_random_names_array
@@ -0,0 +1,36 @@
1
+ module Battleroom
2
+ module Exceptionable
3
+
4
+ def print_colorized_error_prompt(error)
5
+ method_or_variable = (error.class == NoMethodError) ? "method" : "variable"
6
+ battleprint "\nYou're referencing a #{method_or_variable} that doesn't exist, probably as the result of a mispelling. This results in a common Ruby error that reads: \n".red
7
+ battleprint "\tundefined local variable or method \'WHATEVER_YOU_MISTYPED\'\n".green
8
+ battleprint "Get used to it and try again.\n".red
9
+ end
10
+
11
+ def isolate_variable_name_from_name_error(error)
12
+ /`(.+)'/i.match(error.message)
13
+ end
14
+
15
+ def print_unexpected_end_of_input_explanation(error)
16
+ battleprint "\nNope! You just triggered a common Ruby error that reads:\n".red
17
+ battleprint "\tsyntax error, unexpected end-of-input\n".green
18
+ if error.message.include?(']')
19
+ battleprint "Basically, you used an opening square bracket '[', but didn't pair it with a closing square bracket. Try again.\n".red
20
+ else
21
+ battleprint "Basically, you told Ruby you were going to assign a value to a variable, but you neglected to provide a valid value. Try again.\n".red
22
+ end
23
+ end
24
+
25
+ def print_colorized_type_error_prompt(error)
26
+ battleprint "\nNope! You just triggered a common Ruby error that reads:\n".red
27
+ battleprint "\tin '[]', #{error.message}".green
28
+ error.message.match /conversion\sof\s(.+)\sinto\sInteger/i
29
+ battleprint "\nBasically, you put a #{$1} between square brackets, whereas Ruby ".red +
30
+ "was expecting an index value, i.e. an integer. This commonly arises ".red +
31
+ "when programmers think they're dealing with a hash, when in fact ".red +
32
+ "they're dealing with an array. Try again.\n".red
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,82 @@
1
+ module Battleroom
2
+ module Printable
3
+
4
+ CONGRATULATIONS = [
5
+ 'Lovely work, my friend!',
6
+ 'Beautiful!',
7
+ 'Lovely!',
8
+ 'Splendid!',
9
+ 'Awesome.',
10
+ 'Stand up job, my friend.',
11
+ 'Nailed it.',
12
+ 'Nice job! Keep it rolling!',
13
+ 'Capital work, battlestar!',
14
+ 'Exemplary work!',
15
+ 'Yeah!',
16
+ 'Roll on.',
17
+ 'Bullseye!',
18
+ 'Woo!',
19
+ 'Let it ride!',
20
+ 'Touchdown!',
21
+ "You're on your way!",
22
+ "You're making a prosperous go of this programming thing.",
23
+ ]
24
+
25
+ def print_menu_options
26
+ battleprint ' What would you like to work on?'.blue
27
+ battleprint '1. Working with Variables'
28
+ battleprint '2. Accessing Values in Arrays and Hashes'
29
+ battleprint '3. Adding Values to Arrays and Hashes'
30
+ battleprint '4. Working with Methods'
31
+ battleprint "Q. Quit\r\n\n"
32
+ end
33
+
34
+ def random_congratulation
35
+ CONGRATULATIONS.sample
36
+ end
37
+
38
+ def print_congratulation
39
+ battleprint "\n#{random_congratulation}\n".green
40
+ end
41
+
42
+ def indent_all_lines_for_stdout(string_to_indent)
43
+ string_to_indent.gsub(/^.*/) { |match| "\t" + match }
44
+ end
45
+
46
+ def dynamic_word_wrap(string)
47
+ string.wrap(150)
48
+ end
49
+
50
+ def battleprint(string)
51
+ puts dynamic_word_wrap(string)
52
+ end
53
+
54
+ def format_value_for_stdout_and_eval(object)
55
+ case object.class.to_s
56
+ when 'String'
57
+ if object.include?("'")
58
+ '"' + object + '"'
59
+ else
60
+ "'#{object}'"
61
+ end
62
+ when 'Symbol' then return ":#{object}"
63
+ else
64
+ object.to_s
65
+ end
66
+ end
67
+
68
+ def format_class_for_output(klass)
69
+ return 'Boolean' if klass.to_s.match /(TrueClass|FalseClass)/
70
+ klass.to_s
71
+ end
72
+
73
+ def codify(str)
74
+ str.black.on_light_white
75
+ end
76
+
77
+ def colorized_arithmetic_operator_list
78
+ codify(" + ") + ", ".blue + codify(" - ") + ", ".blue + codify(" * ") + ", or ".blue + codify(" / ")
79
+ end
80
+
81
+ end
82
+ end
@@ -5,8 +5,8 @@ class ArrayAccessQuestion < DataStructureAccessQuestion
5
5
 
6
6
  @questions = ARRAY_QUESTIONS.shuffle
7
7
 
8
- def initialize(scope)
9
- super(scope)
8
+ def post_initialize
9
+ super
10
10
  format_array_for_access
11
11
  print_data_structure_access_prompt
12
12
  evaluate_data_structure_access_input
@@ -5,8 +5,8 @@ class ArrayAssignmentQuestion < DataStructureAssignmentQuestion
5
5
 
6
6
  @questions = ARRAY_QUESTIONS
7
7
 
8
- def initialize(scope)
9
- super(scope)
8
+ def post_initialize
9
+ super
10
10
  format_array_for_assignment
11
11
  print_data_structure_assignment_prompt
12
12
  evaluate_data_structure_assignment_input
@@ -2,29 +2,45 @@ require_relative './data_structure_question'
2
2
 
3
3
  class DataStructureAccessQuestion < DataStructureQuestion
4
4
 
5
- def initialize(eval_scope)
6
- super(eval_scope)
7
- end
8
-
9
5
  def print_data_structure_access_prompt
10
6
  answer_value_class = format_class_for_output(answer_value.class)
11
7
  answer_value_string = format_value_for_stdout_and_eval(answer_value)
12
8
  battleprint "Given the data structure below, access the #{answer_value_class} value ".blue + answer_value_string.yellow + ".\n".blue
13
9
  print "#{variable_name} = ".green
14
- if data_structure.to_s.length < 40
15
- battleprint data_structure.to_s.gsub('{', '{ ').gsub('=>', ' => ').gsub('}', ' }')
10
+ if data_structure.to_s.length < 45
11
+ print_data_structure_on_single_line
16
12
  else
17
13
  ap(data_structure, { indent: -2, index: false, multiline: true, plain: true })
18
14
  end
19
15
  battleprint ''
20
16
  end
21
17
 
18
+ def provide_evaluation_scope_with_variable_assignment_necessary_for_answer_eval
19
+ evaluation_scope.eval("#{variable_name} = #{data_structure.to_s}")
20
+ end
21
+
22
+ def generate_readable_single_line_hash
23
+ data_structure.to_s.gsub('{', '{ ').gsub('=>', ' => ').gsub('}', ' }')
24
+ end
25
+
26
+ def print_data_structure_on_single_line
27
+ single_line_hash_as_string = generate_readable_single_line_hash()
28
+ battleprint(single_line_hash_as_string)
29
+ end
30
+
31
+ def eval_returns_the_correct_value?(user_input)
32
+ evaluation_scope.eval(user_input) == answer_value
33
+ end
34
+
35
+ def user_input_uses_variable?(user_input)
36
+ user_input.include?(variable_name)
37
+ end
38
+
22
39
  def evaluate_data_structure_access_input
23
40
  enter_evaluation_loop do |user_submission|
24
41
  begin
25
- # provides the evaluation scope with variable assignment necessary for answer eval
26
- evaluation_scope.eval("#{variable_name} = #{data_structure.to_s}")
27
- if evaluation_scope.eval(user_submission) == answer_value && user_submission.include?(variable_name)
42
+ provide_evaluation_scope_with_variable_assignment_necessary_for_answer_eval()
43
+ if eval_returns_the_correct_value?(user_submission) && user_input_uses_variable?(user_submission)
28
44
  true
29
45
  else
30
46
  battleprint "Remember, #{hint} Try again.".red
@@ -19,8 +19,8 @@ class DataStructureAssignmentQuestion < DataStructureQuestion
19
19
  'Terrific'
20
20
  ]
21
21
 
22
- def initialize(eval_scope)
23
- super(eval_scope)
22
+ def post_initialize
23
+ super
24
24
  end
25
25
 
26
26
  def print_data_structure
@@ -3,8 +3,7 @@ require_relative './question'
3
3
  class DataStructureQuestion < Question
4
4
  attr_accessor :data_structure, :hint, :possible_assignments
5
5
 
6
- def initialize(eval_scope)
7
- super(eval_scope)
6
+ def post_initialize
8
7
  @data_structure = data[:data_structure].clone
9
8
  if data_structure.class == Array
10
9
  # randomizes and shuffles the items in the arrays, so repeats remain interesting
@@ -8,6 +8,11 @@ class FollowUpQuestion < Question
8
8
  @evaluation_scope = evaluation_scope
9
9
  @original_question = original_question
10
10
  @input_mechanism = 'readline'
11
+ post_initialize
12
+ end
13
+
14
+ def post_initialize
15
+ nil
11
16
  end
12
17
 
13
18
  end
@@ -5,8 +5,8 @@ class HashAccessQuestion < DataStructureAccessQuestion
5
5
 
6
6
  @questions = HASH_QUESTIONS.shuffle
7
7
 
8
- def initialize(scope)
9
- super(scope)
8
+ def post_initialize
9
+ super
10
10
  format_hash
11
11
  print_data_structure_access_prompt
12
12
  evaluate_data_structure_access_input
@@ -14,12 +14,12 @@ class HashAccessQuestion < DataStructureAccessQuestion
14
14
 
15
15
  def format_hash
16
16
  cull_hash_to_valid_size_for_output
17
- remove_multiple_booleans
17
+ remove_multiple_booleans_to_avoid_possibility_of_ambiguous_prompt
18
18
  self.answer_value = data_structure[data_structure.keys.sample]
19
19
  self.hint = 'you have to use the EXACT hash key to retrieve the associated value.'
20
20
  end
21
21
 
22
- def remove_multiple_booleans
22
+ def remove_multiple_booleans_to_avoid_possibility_of_ambiguous_prompt
23
23
  boolean_count = find_number_of_boolean_values_in_hash
24
24
  while boolean_count > 1
25
25
  grouped_by_value = data_structure.group_by { |k, v| v }
@@ -5,8 +5,8 @@ class HashAssignmentQuestion < DataStructureAssignmentQuestion
5
5
 
6
6
  @questions = HASH_QUESTIONS.shuffle
7
7
 
8
- def initialize(scope)
9
- super(scope)
8
+ def post_initialize
9
+ super
10
10
  format_hash_for_assignment
11
11
  print_data_structure_assignment_prompt
12
12
  evaluate_data_structure_assignment_input
@@ -14,11 +14,11 @@ class HashAssignmentQuestion < DataStructureAssignmentQuestion
14
14
 
15
15
  def format_hash_for_assignment
16
16
  cull_hash_to_valid_size_for_output
17
- assignment = possible_assignments.sample
18
- self.assignment_value = assignment.values[0]
19
- self.assignment_value_class = format_class_for_output(assignment_value.class)
17
+ assignment = possible_assignments.sample
18
+ self.assignment_value = assignment.values[0]
19
+ self.assignment_value_class = format_class_for_output(assignment_value.class)
20
20
  self.formatted_assignment_value = format_value_for_stdout_and_eval(assignment_value)
21
- self.assignment_key = format_value_for_stdout_and_eval(assignment.keys[0])
21
+ self.assignment_key = format_value_for_stdout_and_eval(assignment.keys[0])
22
22
  end
23
23
 
24
24
  def print_data_structure_assignment_prompt
@@ -1,102 +1,101 @@
1
1
  require_relative '../data/method_questions'
2
2
  require_relative './question'
3
3
 
4
- # class MethodDefinitionQuestion < Question
4
+ class MethodDefinitionQuestion < Question
5
5
 
6
- # attr_accessor :method_name, :arg_count, :spec, :eval_string, :eval_answer,
7
- # :return_value, :user_answer_verified
6
+ attr_accessor :method_name, :arg_count, :spec, :eval_string, :eval_answer,
7
+ :return_value, :user_answer_verified
8
8
 
9
- # @questions = METHOD_QUESTONS.shuffle
9
+ @questions = METHOD_QUESTONS.shuffle
10
10
 
11
- # def initialize(scope)
12
- # super(scope)
13
- # @method_name = data[:method_name]
14
- # @arg_count = data[:arg_count]
15
- # @spec = data[:spec]
16
- # @eval_string = data[:eval_string]
17
- # @eval_answer = data[:eval_answer]
18
- # @input_mechanism = 'pry'
19
- # print_prompt
20
- # evaluate_method_definition_input
21
- # end
11
+ def post_initialize
12
+ @method_name = data[:method_name]
13
+ @arg_count = data[:arg_count]
14
+ @spec = data[:spec]
15
+ @eval_string = data[:eval_string]
16
+ @eval_answer = data[:eval_answer]
17
+ @input_mechanism = 'pry'
18
+ print_prompt
19
+ evaluate_method_definition_input
20
+ end
22
21
 
23
- # def print_prompt
24
- # battleprint('Define a method called '.blue + method_name.yellow + ' that takes '.blue + arg_count.to_s.yellow + " argument(s) and #{spec}.\n".blue)
25
- # end
22
+ def print_prompt
23
+ battleprint('Define a method called '.blue + method_name.yellow + ' that takes '.blue + arg_count.to_s.yellow + " argument(s) and #{spec}.\n".blue)
24
+ end
26
25
 
27
- # def handle_name_error_exceptions(error, user_submission)
28
- # if user_submission.include?('def') == false
29
- # print_no_method_error_prompt
30
- # else
31
- # print_colorized_error_prompt(error)
32
- # end
33
- # end
26
+ def handle_name_error_exceptions(error, user_submission)
27
+ if user_submission.include?('def') == false
28
+ print_no_method_error_prompt
29
+ else
30
+ print_colorized_error_prompt(error)
31
+ end
32
+ end
34
33
 
35
- # def handle_incorrect_method_definition(return_value)
36
- # battleprint "\nWhen invoking your method via ".red + eval_string + ", your method returned #{return_value || 'nil'}. It should have returned #{eval_answer}. Try again.\n".red
37
- # end
34
+ def handle_incorrect_method_definition(return_value)
35
+ battleprint "\nWhen invoking your method via ".red + eval_string + ", your method returned #{return_value || 'nil'}. It should have returned #{eval_answer}. Try again.\n".red
36
+ end
38
37
 
39
- # def print_puts_explanation
40
- # battleprint 'The last line of your method definition uses Ruby\'s "puts" method. The "puts" method is helpful for logging errors and statuses to the console, but its actual '.red + "return".red.underline + " value is always nil, and thus your method returns nil. Try again, this time without using \"puts\".\n".red
41
- # end
38
+ def print_puts_explanation
39
+ battleprint 'The last line of your method definition uses Ruby\'s "puts" method. The "puts" method is helpful for logging errors and statuses to the console, but its actual '.red + "return".red.underline + " value is always nil, and thus your method returns nil. Try again, this time without using \"puts\".\n".red
40
+ end
42
41
 
43
- # def print_no_method_error_prompt
44
- # battleprint "\nYou just trigged a common Ruby error that reads: \n".red
45
- # battleprint "\tundefined method \'WHATEVER_METHOD_YOU_TRIED_TO_INVOKE\'\n".green
46
- # battleprint "Basically, you tried to use a method before you defined it, and Ruby said, \"You haven't told me how to do that yet.\" To let Ruby know that you're defining a method, you'll use the the \"def\" keyword, followed by the desired method name, and in many cases an optional list of arguments between parentheses. Once you've finished writing the method's inner code, you'll let Ruby know that your method definition is finished by using the \"end\" keyword.\n".red
47
- # end
42
+ def print_no_method_error_prompt
43
+ battleprint "\nYou just trigged a common Ruby error that reads: \n".red
44
+ battleprint "\tundefined method \'WHATEVER_METHOD_YOU_TRIED_TO_INVOKE\'\n".green
45
+ battleprint "Basically, you tried to use a method before you defined it, and Ruby said, \"You haven't told me how to do that yet.\" To let Ruby know that you're defining a method, you'll use the the \"def\" keyword, followed by the desired method name, and in many cases an optional list of arguments between parentheses. Once you've finished writing the method's inner code, you'll let Ruby know that your method definition is finished by using the \"end\" keyword.\n".red
46
+ end
48
47
 
49
- # def fresh_binding
50
- # binding
51
- # end
48
+ def fresh_binding
49
+ binding
50
+ end
52
51
 
53
- # def print_wrong_method_error(error, user_submission)
54
- # definition_pattern = Regexp.new("def\s*#{method_name}")
55
- # if user_submission.match(definition_pattern)
56
- # handle_incorrect_method_definition(user_submission)
57
- # else
58
- # battleprint "\nYou defined the wrong method, probably as the result of a mispelling. Try again.".red
59
- # end
60
- # end
52
+ def print_wrong_method_error(error, user_submission)
53
+ definition_pattern = Regexp.new("def\s*#{method_name}")
54
+ if user_submission.match(definition_pattern)
55
+ handle_incorrect_method_definition(user_submission)
56
+ else
57
+ battleprint "\nYou defined the wrong method, probably as the result of a mispelling. Try again.".red
58
+ end
59
+ end
61
60
 
62
- # def print_argument_error_prompt(e)
63
- # e.message.match(/wrong number of arguments \((\d) for (\d)\)/)
64
- # passed_arg_count = $1.to_i
65
- # expected_arg_count = $2.to_i
66
- # battleprint "\nLooks like you defined #{method_name} to take #{expected_arg_count} argument(s), when it should take #{arg_count}. Try again.\n".red
67
- # end
61
+ def print_argument_error_prompt(e)
62
+ e.message.match(/wrong number of arguments \((\d) for (\d)\)/)
63
+ passed_arg_count = $1.to_i
64
+ expected_arg_count = $2.to_i
65
+ battleprint "\nLooks like you defined #{method_name} to take #{expected_arg_count} argument(s), when it should take #{arg_count}. Try again.\n".red
66
+ end
68
67
 
69
- # def clean_eval_scope_of_method_definition
70
- # if evaluation_scope.eval "respond_to?(:#{method_name}, true)"
71
- # evaluation_scope.eval 'Object.class_eval("remove_method :' + method_name + '")'
72
- # end
73
- # end
68
+ def clean_eval_scope_of_method_definition
69
+ if evaluation_scope.eval "respond_to?(:#{method_name}, true)"
70
+ evaluation_scope.eval 'Object.class_eval("remove_method :' + method_name + '")'
71
+ end
72
+ end
74
73
 
75
- # def evaluate_method_definition_input
76
- # enter_evaluation_loop do |user_submission|
77
- # begin
78
- # clean_eval_scope_of_method_definition
79
- # # I want to make sure that the user's method isn't invoked if it uses the puts method
80
- # if user_submission.include?('puts')
81
- # print_puts_explanation
82
- # else
83
- # evaluation_scope.eval(user_submission)
84
- # return_value = evaluation_scope.eval(eval_string)
85
- # if (return_value == eval_answer)
86
- # self.user_answer_verified = user_submission
87
- # true
88
- # else
89
- # handle_incorrect_method_definition(return_value)
90
- # end
91
- # end
92
- # rescue ArgumentError => e
93
- # print_argument_error_prompt(e)
94
- # rescue NoMethodError => e
95
- # print_wrong_method_error(e, user_submission)
96
- # rescue NameError => e
97
- # handle_name_error_exceptions(e, user_submission)
98
- # end
99
- # end
100
- # end
74
+ def evaluate_method_definition_input
75
+ enter_evaluation_loop do |user_submission|
76
+ begin
77
+ clean_eval_scope_of_method_definition
78
+ # I want to make sure that the user's method isn't invoked if it uses the puts method
79
+ if user_submission.include?('puts')
80
+ print_puts_explanation
81
+ else
82
+ evaluation_scope.eval(user_submission)
83
+ return_value = evaluation_scope.eval(eval_string)
84
+ if (return_value == eval_answer)
85
+ self.user_answer_verified = user_submission
86
+ true
87
+ else
88
+ handle_incorrect_method_definition(return_value)
89
+ end
90
+ end
91
+ rescue ArgumentError => e
92
+ print_argument_error_prompt(e)
93
+ rescue NoMethodError => e
94
+ print_wrong_method_error(e, user_submission)
95
+ rescue NameError => e
96
+ handle_name_error_exceptions(e, user_submission)
97
+ end
98
+ end
99
+ end
101
100
 
102
- # end
101
+ end
@@ -1,90 +1,89 @@
1
1
  require_relative './follow_up_question'
2
2
 
3
- # class MethodInvocationQuestion < FollowUpQuestion
3
+ class MethodInvocationQuestion < FollowUpQuestion
4
4
 
5
- # attr_reader :desired_answer_formatted, :desired_answer_class_formatted
5
+ attr_reader :desired_answer_formatted, :desired_answer_class_formatted
6
6
 
7
- # def initialize(evaluation_scope, question_to_follow_up_on)
8
- # super(evaluation_scope, question_to_follow_up_on)
9
- # @desired_answer_formatted = format_value_for_stdout_and_eval(original_question.eval_answer)
10
- # @desired_answer_class_formatted = format_class_for_output(original_question.eval_answer.class)
11
- # print_method_invocation_prompt
12
- # evaluate_user_input
13
- # end
7
+ def post_initialize
8
+ @desired_answer_formatted = format_value_for_stdout_and_eval(original_question.eval_answer)
9
+ @desired_answer_class_formatted = format_class_for_output(original_question.eval_answer.class)
10
+ print_method_invocation_prompt
11
+ evaluate_user_input
12
+ end
14
13
 
15
- # def format_method_definition_for_stdout
16
- # code = CodeRay.scan($input, :ruby)
17
- # ansi_prepped_string = code.term
18
- # indent_all_lines_for_stdout(ansi_prepped_string)
19
- # end
14
+ def format_method_definition_for_stdout
15
+ code = CodeRay.scan($input, :ruby)
16
+ ansi_prepped_string = code.term
17
+ indent_all_lines_for_stdout(ansi_prepped_string)
18
+ end
20
19
 
21
- # def print_method_invocation_prompt
22
- # battleprint "You now have the method defined below at your disposal.\n".blue
23
- # battleprint format_method_definition_for_stdout
24
- # battleprint "\nInvoke the '#{original_question.method_name}' method such that it returns the ".blue + desired_answer_class_formatted.blue + " value ".blue + desired_answer_formatted.yellow + ".\n".blue
25
- # end
20
+ def print_method_invocation_prompt
21
+ battleprint "You now have the method defined below at your disposal.\n".blue
22
+ battleprint format_method_definition_for_stdout
23
+ battleprint "\nInvoke the '#{original_question.method_name}' method such that it returns the ".blue + desired_answer_class_formatted.blue + " value ".blue + desired_answer_formatted.yellow + ".\n".blue
24
+ end
26
25
 
27
- # def print_no_method_error_prompt
28
- # battleprint "\nYou just trigged a common Ruby error that reads: \n".red
29
- # battleprint "\tundefined local variable or method \'WHATEVER_METHOD_YOU_TRIED_TO_INVOKE\'\n".green
30
- # battleprint "Basically, you tried to invoke a method that doesn't exist. Try again.\n".red
31
- # end
26
+ def print_no_method_error_prompt
27
+ battleprint "\nYou just trigged a common Ruby error that reads: \n".red
28
+ battleprint "\tundefined local variable or method \'WHATEVER_METHOD_YOU_TRIED_TO_INVOKE\'\n".green
29
+ battleprint "Basically, you tried to invoke a method that doesn't exist. Try again.\n".red
30
+ end
32
31
 
33
- # def print_name_error_prompt(error, user_submission)
34
- # battleprint "\nYou just triggered a common Ruby error that reads:\n".red
35
- # battleprint "\tNameError: #{error.message}\n".green
36
- # referenced_variable = isolate_variable_name_from_name_error(error)
37
- # parameters = isolate_argument_names_from_method_def
38
- # if parameters.find { |parameter| parameter == referenced_variable }
39
- # battleprint "Looks like the variable you referenced was one of the argument names in your method #{'definition'.red.underline}. The arguments in method definitions are just placeholders for whatever values end up getting passed in when the method is called. Think on this for a moment before trying again.\n".red
40
- # else
41
- # battleprint "Basically, you're referencing a variable, \"#{referenced_variable}\", that hasn't been assigned a value.\n".red
42
- # end
43
- # end
32
+ def print_name_error_prompt(error, user_submission)
33
+ battleprint "\nYou just triggered a common Ruby error that reads:\n".red
34
+ battleprint "\tNameError: #{error.message}\n".green
35
+ referenced_variable = isolate_variable_name_from_name_error(error)
36
+ parameters = isolate_argument_names_from_method_def
37
+ if parameters.find { |parameter| parameter == referenced_variable }
38
+ battleprint "Looks like the variable you referenced was one of the argument names in your method #{'definition'.red.underline}. The arguments in method definitions are just placeholders for whatever values end up getting passed in when the method is called. Think on this for a moment before trying again.\n".red
39
+ else
40
+ battleprint "Basically, you're referencing a variable, \"#{referenced_variable}\", that hasn't been assigned a value.\n".red
41
+ end
42
+ end
44
43
 
45
- # def isolate_variable_name_from_name_error(error)
46
- # error.name.to_s
47
- # end
44
+ def isolate_variable_name_from_name_error(error)
45
+ error.name.to_s
46
+ end
48
47
 
49
- # def print_argument_error_prompt(e)
50
- # e.message.match(/wrong number of arguments \((\d) for (\d)\)/)
51
- # passed_arg_count = $1.to_i
52
- # expected_arg_count = $2.to_i
53
- # battleprint "You just triggered a common Ruby error that reads:\n".red
54
- # battleprint "\tArgumentError: #{e.message}\n".green
55
- # battleprint "Basically, you defined the '#{original_question.method_name}' method to expect #{expected_arg_count} argument(s), and you're only passing it #{passed_arg_count}. Try again.\n".red
56
- # end
48
+ def print_argument_error_prompt(e)
49
+ e.message.match(/wrong number of arguments \((\d) for (\d)\)/)
50
+ passed_arg_count = $1.to_i
51
+ expected_arg_count = $2.to_i
52
+ battleprint "You just triggered a common Ruby error that reads:\n".red
53
+ battleprint "\tArgumentError: #{e.message}\n".green
54
+ battleprint "Basically, you defined the '#{original_question.method_name}' method to expect #{expected_arg_count} argument(s), and you're only passing it #{passed_arg_count}. Try again.\n".red
55
+ end
57
56
 
58
- # def isolate_argument_names_from_method_def
59
- # isolate_parameter_list_string_from_method_def.scan(/([^,\s]+)+/x).flatten
60
- # end
57
+ def isolate_argument_names_from_method_def
58
+ isolate_parameter_list_string_from_method_def.scan(/([^,\s]+)+/x).flatten
59
+ end
61
60
 
62
- # def isolate_parameter_list_string_from_method_def
63
- # original_question.user_answer_verified.match /\((.+)\)/
64
- # $1
65
- # end
61
+ def isolate_parameter_list_string_from_method_def
62
+ original_question.user_answer_verified.match /\((.+)\)/
63
+ $1
64
+ end
66
65
 
67
- # def print_incorrect_input_prompt(return_from_eval)
68
- # battleprint "Your code returned the #{format_class_for_output(return_from_eval.class)} value #{return_from_eval.to_s} when it should have returned the #{desired_answer_class_formatted} value #{desired_answer_formatted}. Try again.".red
69
- # end
66
+ def print_incorrect_input_prompt(return_from_eval)
67
+ battleprint "Your code returned the #{format_class_for_output(return_from_eval.class)} value #{return_from_eval.to_s} when it should have returned the #{desired_answer_class_formatted} value #{desired_answer_formatted}. Try again.".red
68
+ end
70
69
 
71
- # def evaluate_user_input
72
- # enter_evaluation_loop do |user_submission|
73
- # begin
74
- # return_from_eval = original_question.evaluation_scope.eval(user_submission)
75
- # if return_from_eval == original_question.eval_answer
76
- # true
77
- # else
78
- # print_incorrect_input_prompt(return_from_eval)
79
- # end
80
- # rescue NoMethodError => e
81
- # print_no_method_error_prompt
82
- # rescue NameError => e
83
- # print_name_error_prompt(e, user_submission)
84
- # rescue ArgumentError => e
85
- # print_argument_error_prompt(e)
86
- # end
87
- # end
88
- # end
70
+ def evaluate_user_input
71
+ enter_evaluation_loop do |user_submission|
72
+ begin
73
+ return_from_eval = original_question.evaluation_scope.eval(user_submission)
74
+ if return_from_eval == original_question.eval_answer
75
+ true
76
+ else
77
+ print_incorrect_input_prompt(return_from_eval)
78
+ end
79
+ rescue NoMethodError => e
80
+ print_no_method_error_prompt
81
+ rescue NameError => e
82
+ print_name_error_prompt(e, user_submission)
83
+ rescue ArgumentError => e
84
+ print_argument_error_prompt(e)
85
+ end
86
+ end
87
+ end
89
88
 
90
- # end
89
+ end
@@ -6,8 +6,7 @@ class NestedDataStructureAccessQuestion < DataStructureAccessQuestion
6
6
  # class instance variable used within the initialize method for Question
7
7
  @questions = NESTED_DATA_STRUCTURE_ACCESS_QUESTIONS.shuffle
8
8
 
9
- def initialize(eval_scope)
10
- super(eval_scope)
9
+ def post_initialize
11
10
  if data_structure.class == Array
12
11
  self.inner_hash = data_structure.sample
13
12
  self.answer_value = inner_hash[inner_hash.keys.sample]
@@ -9,10 +9,14 @@ class Question
9
9
  @data = self.class.generate_question
10
10
  @variable_name = rotate_array(data[:possible_variable_names] || []).first
11
11
  @input_mechanism = 'readline'
12
+ post_initialize
12
13
  end
13
14
 
14
- # retrieves question from front of the array and rotates it to the back
15
- # to avoid immediate re-sampling of questions
15
+ def post_initialize
16
+ nil
17
+ end
18
+
19
+ # avoids immediate re-sampling of questions
16
20
  def self.generate_question
17
21
  # calls upon *class instance* variable assigned in the subclasses
18
22
  question = @questions.shift
@@ -6,8 +6,7 @@ class VariableAssignmentQuestion < Question
6
6
  attr_accessor :formatted_value, :formatted_class
7
7
  @questions = VARIABLE_QUESTIONS.shuffle
8
8
 
9
- def initialize(eval_scope)
10
- super(eval_scope)
9
+ def post_initialize
11
10
  @variable_value = rotate_array(data[:possible_variable_values]).first
12
11
  @formatted_value = format_value_for_stdout_and_eval(variable_value)
13
12
  print_variable_assignment_prompt
@@ -4,8 +4,7 @@ class VariableReassignmentQuestion < FollowUpQuestion
4
4
 
5
5
  attr_accessor :toggled_boolean
6
6
 
7
- def initialize(evaluation_scope, original_question)
8
- super(evaluation_scope, original_question)
7
+ def post_initialize
9
8
  @toggled_boolean = !(original_question.variable_value)
10
9
  print_variable_reassignment_prompt
11
10
  evaluate_variable_reassignment_input
@@ -18,11 +17,15 @@ class VariableReassignmentQuestion < FollowUpQuestion
18
17
  def evaluate_variable_reassignment_input
19
18
  enter_evaluation_loop do |user_submission|
20
19
  input = user_submission
21
- evaluation_scope.eval(input)
22
- if (evaluation_scope.eval(original_question.variable_name) == toggled_boolean)
23
- true
24
- else
25
- battleprint "Nope. Try again.".red
20
+ begin
21
+ evaluation_scope.eval(input)
22
+ if (evaluation_scope.eval(original_question.variable_name) == toggled_boolean)
23
+ true
24
+ else
25
+ battleprint "Nope. Try again.".red
26
+ end
27
+ rescue NameError, NoMethodError => e
28
+ print_colorized_error_prompt(e)
26
29
  end
27
30
  end
28
31
  end
@@ -2,54 +2,62 @@ require_relative './follow_up_question'
2
2
 
3
3
  class VariableReferenceQuestion < FollowUpQuestion
4
4
 
5
- attr_accessor :required_return_value
5
+ attr_accessor :required_return_value, :required_class
6
+ attr_reader :variable_name, :variable_value, :formatted_variable_value,
7
+ :formatted_variable_class
6
8
 
7
- def initialize(scope, original_question)
8
- super(scope, original_question)
9
+ def post_initialize
10
+ @variable_name = original_question.variable_name
11
+ @variable_value = original_question.variable_value
12
+ @formatted_variable_value = original_question.formatted_value
13
+ @formatted_variable_class = original_question.formatted_class
9
14
  print_variable_reference_prompt
10
15
  evaluate_variable_reference_input
11
16
  end
12
17
 
13
18
  def print_variable_reference_prompt
14
19
  battleprint "You now have the variable assignment below at your disposal.\n".blue
15
- battleprint "\t#{original_question.variable_name} = #{original_question.formatted_value}\n\n"
16
- prompt = develop_prompt
20
+ battleprint "\t#{variable_name} = #{formatted_variable_value}\n\n"
21
+ prompt = develop_prompt()
17
22
  puts prompt
18
23
  end
19
24
 
20
25
  def a_value_within_12_more_or_12_less_than_the_original_assigned_value
21
26
  range = (-12..12).to_a
22
27
  range.delete(0)
23
- original_question.variable_value + range.sample
28
+ variable_value + range.sample
24
29
  end
25
30
 
26
31
  def a_value_that_doesnt_result_from_imprecise_float_arithmetic
27
- if original_question.variable_value < 15
28
- new_target_value = original_question.variable_value + rand(1..15)
32
+ if variable_value < 15
33
+ new_target_value = variable_value + rand(1..15)
29
34
  else
30
35
  new_target_value = a_value_within_12_more_or_12_less_than_the_original_assigned_value
31
36
  end
32
37
  count = 1
33
38
  while new_target_value.to_s.split(".").last.length > 4
34
- new_target_value = original_question.variable_value + count
39
+ new_target_value = variable_value + count
35
40
  count += 1
36
41
  end
37
42
  new_target_value
38
43
  end
39
44
 
45
+ def a_value_that_requires_low_arithmetic_complexity_to_arrive_at_via_multiplication_or_division
46
+ if variable_value < 12
47
+ variable_value * [3,4,5,6].sample
48
+ elsif variable_value < 50
49
+ variable_value * 2
50
+ else
51
+ variable_value / 2
52
+ end
53
+ end
54
+
40
55
  def generate_appropriate_value
41
- value = original_question.variable_value
42
- if value.class == Fixnum
43
- if value.even? # && [1,2,3,4,5].sample.even?
44
- if value < 12
45
- value * [3,4,5,6].sample
46
- elsif value < 50
47
- value * 2
48
- else
49
- value / 2
50
- end
56
+ if variable_value.class == Fixnum
57
+ if variable_value.even?
58
+ a_value_that_requires_low_arithmetic_complexity_to_arrive_at_via_multiplication_or_division()
51
59
  else
52
- a_value_within_12_more_or_12_less_than_the_original_assigned_value
60
+ a_value_within_12_more_or_12_less_than_the_original_assigned_value()
53
61
  end
54
62
  else
55
63
  a_value_that_doesnt_result_from_imprecise_float_arithmetic()
@@ -57,17 +65,22 @@ class VariableReferenceQuestion < FollowUpQuestion
57
65
  end
58
66
 
59
67
  def develop_prompt
60
- case original_question.variable_value.class.to_s
61
- when 'Fixnum', 'Float'
62
- self.required_return_value = generate_appropriate_value()
63
- 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
64
- else
65
- battleprint "Something's gone wrong. This code should never run.".red
66
- end
68
+ self.required_return_value = generate_appropriate_value()
69
+ required_return_value_colorized = required_return_value.to_s.green
70
+ self.required_class = original_question.formatted_class
71
+ "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
67
72
  end
68
73
 
69
74
  def provide_evaluation_scope_with_original_variable_assignment
70
- evaluation_scope.eval("#{original_question.variable_name} = #{original_question.formatted_value}")
75
+ evaluation_scope.eval("#{variable_name} = #{formatted_variable_value}")
76
+ end
77
+
78
+ def user_doesnt_use_variable?(user_input)
79
+ !user_input.include?(variable_name)
80
+ end
81
+
82
+ def coach_user_and_reprompt(return_value_of_user_input)
83
+ 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
71
84
  end
72
85
 
73
86
  def evaluate_variable_reference_input
@@ -75,14 +88,14 @@ class VariableReferenceQuestion < FollowUpQuestion
75
88
  begin
76
89
  provide_evaluation_scope_with_original_variable_assignment
77
90
  returned_value = evaluation_scope.eval(user_submission)
78
- if !user_submission.include?(original_question.variable_name)
79
- battleprint "You didn't make use of the '#{original_question.variable_name}' variable, which is the entire purpose of this exercise. Try again.".red
91
+ if user_doesnt_use_variable?(user_submission)
92
+ battleprint "You didn't make use of the '#{variable_name}' variable, which is the entire purpose of this exercise. Try again.".red
80
93
  elsif user_submission.include?("=")
81
- 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
94
+ 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
82
95
  elsif (returned_value == required_return_value)
83
96
  true
84
97
  else
85
- 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
98
+ coach_user_and_reprompt(returned_value)
86
99
  end
87
100
  rescue NameError, NoMethodError => e
88
101
  print_colorized_error_prompt(e)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: battleroom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.87
4
+ version: 0.0.871
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Vander Hoop
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.3
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.3
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: coderay
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -128,9 +142,9 @@ files:
128
142
  - lib/battleroom/data/nested_data_structure_access_questions.rb
129
143
  - lib/battleroom/data/scratch.rb
130
144
  - lib/battleroom/data/variable_assignment_questions.rb
131
- - lib/battleroom/data_generation_machinery.rb
132
- - lib/battleroom/helpers/array_formatter.rb
133
- - lib/battleroom/helpers/hash_formatter.rb
145
+ - lib/battleroom/helpers/data_generation_machinery.rb
146
+ - lib/battleroom/helpers/exceptionable.rb
147
+ - lib/battleroom/helpers/printable.rb
134
148
  - lib/battleroom/models/array_access_question.rb
135
149
  - lib/battleroom/models/array_assignment_question.rb
136
150
  - lib/battleroom/models/data_structure_access_question.rb
@@ -158,7 +172,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
158
172
  requirements:
159
173
  - - ">="
160
174
  - !ruby/object:Gem::Version
161
- version: '2.1'
175
+ version: '2.0'
162
176
  required_rubygems_version: !ruby/object:Gem::Requirement
163
177
  requirements:
164
178
  - - ">="
@@ -1,3 +0,0 @@
1
- module ArrayFormatter
2
-
3
- end
@@ -1,3 +0,0 @@
1
- module HashFormatter
2
-
3
- end