battleroom 0.0.78 → 0.0.79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d4723d3ecf5bca3b1bf87337f5e1b7cbc5b20310
4
- data.tar.gz: 1f898090ed5bd802f5d4c46507fcabc0861bcf8e
3
+ metadata.gz: 340c1d7b9d05a1f38b513a5d48f3fd0ab96b1547
4
+ data.tar.gz: ab44f38b9d800263c0b4a9c72390535aa9de6fee
5
5
  SHA512:
6
- metadata.gz: e6999ae0f4f292e1335571ba70d10dc331832f4336842552799b63b260362360def2e0afe5b341a571a402b99b81ba4e71954f29fa8f1d6e9dade524e1e6d5e9
7
- data.tar.gz: 7ba16ba01aaaa31b9cfca07f933b92bfca19065036531f59f3daf38e56113013260012ccf9f8e8cba5d3368d291f6c805df55686335f9dc27dd4e0295b054e1f
6
+ metadata.gz: 8b1547328ed465de041958c01675f2a32e64862c8edb38213c8107ffd33ca1e49801ecad951e0ee1311d09c52d8944493577b1dfddd4c0e123b5ce8004bbc329
7
+ data.tar.gz: 84eb2f9983d8fd8706a1005c58bbe2517b6b44156c2d7ff1f409e670f8ad572452b86c9b4f63d6ddcf25ff2c9571af17a70a66c7d6cd482451f3b0d4bd19dd76
data/lib/battleroom.rb CHANGED
@@ -66,7 +66,7 @@ loop do
66
66
  q = MethodDefinitionQuestion.new(b)
67
67
  q.print_prompt
68
68
  q.evaluate_method_definition_input
69
- follow_up_question = MethodDefinitionFollowUpQuestion.new(b, q)
69
+ follow_up_question = MethodInvocationQuestion.new(b, q)
70
70
  follow_up_question.print_method_invocation_prompt
71
71
  follow_up_question.evaluate_user_input
72
72
  end
@@ -0,0 +1,29 @@
1
+ area_arg_one = rand(1..9)
2
+ area_arg_two = rand(1..9)
3
+ volume_arg_one = rand(1..9)
4
+ volume_arg_two = rand(1..9)
5
+ volume_arg_three = rand(1..9)
6
+
7
+ METHOD_QUESTONS = [
8
+ {
9
+ method_name: "calc_area",
10
+ arg_count: 2,
11
+ spec: "returns the product of its two arguments",
12
+ eval_string: "calc_area(#{area_arg_one}, #{area_arg_two})",
13
+ eval_answer: area_arg_one * area_arg_two
14
+ },
15
+ {
16
+ method_name: "calc_volume",
17
+ arg_count: 3,
18
+ spec: "returns the product of its three arguments",
19
+ eval_string: "calc_volume(#{volume_arg_one}, #{volume_arg_two}, #{volume_arg_three})",
20
+ eval_answer: volume_arg_one * volume_arg_two * volume_arg_three
21
+ },
22
+ {
23
+ method_name: "sum",
24
+ arg_count: 2,
25
+ spec: "returns the sum of its two arguments",
26
+ eval_string: "sum(#{area_arg_one}, #{area_arg_two})",
27
+ eval_answer: area_arg_one + area_arg_two
28
+ },
29
+ ]
@@ -0,0 +1,73 @@
1
+ require_relative './question'
2
+
3
+ class MethodInvocationQuestion < Question
4
+
5
+ attr_reader :original_question, :desired_answer_formatted, :desired_answer_class_formatted
6
+
7
+ def initialize(evaluation_scope, question_to_follow_up_on)
8
+ @evaluation_scope = evaluation_scope
9
+ @original_question = question_to_follow_up_on
10
+ @desired_answer_formatted = format_value_for_stdout_and_eval(original_question.eval_answer)
11
+ @desired_answer_class_formatted = format_class_for_output(original_question.eval_answer.class)
12
+ end
13
+
14
+ def format_method_definition_for_stdout
15
+ code = CodeRay.scan($input, :ruby)
16
+ # adds indentation to all lines of method definition
17
+ code.term.gsub(/^.*/) { |match| "\t" + match }
18
+ end
19
+
20
+ def print_method_invocation_prompt
21
+ puts "You now have the method defined below at your disposal.\n".blue
22
+ puts format_method_definition_for_stdout
23
+ puts "\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\n"
24
+ end
25
+
26
+ def print_no_method_error_prompt
27
+ puts "\nYou just trigged a common Ruby error that reads: \n".red
28
+ puts "\tundefined local variable or method \'WHATEVER_METHOD_YOU_TRIED_TO_INVOKE\'\n".green
29
+ puts "Basically, you tried to invoke a method that doesn't exist. Try again.\n".red
30
+ end
31
+
32
+ def print_name_error_prompt(error, user_submission)
33
+ puts "You just triggered a common Ruby error that reads:\n".red
34
+ puts "\tNameError: #{error.message}\n".green
35
+ /`(.+)'/i.match(error.message)
36
+ referenced_variable = $1
37
+ passed_as_argument_pattern = Regexp.new("\(.*#{$1}.*\)")
38
+ if user_submission.match(passed_as_argument_pattern)
39
+ puts "You're trying to pass the '#{original_question.method_name}' method the value stored in the variable '#{referenced_variable}', but that variable hasn't been assigned a value.\n".red
40
+ else
41
+ puts "Basically, you're referencing a variable, #{$1}, that hasn't been defined.\n".red
42
+ end
43
+ end
44
+
45
+ def print_argument_error_prompt(e)
46
+ e.message.match(/wrong number of arguments \((\d) for (\d)\)/)
47
+ passed_arg_count = $1.to_i
48
+ expected_arg_count = $2.to_i
49
+ puts "You just triggered a common Ruby error that reads:\n".red
50
+ puts "\tArgumentError: #{e.message}\n".green
51
+ puts "Basically, you defined the '#{original_question.method_name}' method to expect #{expected_arg_count} argument(s), and you're only passing #{passed_arg_count}. Try again.\n".red
52
+ end
53
+
54
+ def evaluate_user_input
55
+ enter_evaluation_loop do |user_submission|
56
+ begin
57
+ return_from_eval = original_question.evaluation_scope.eval(user_submission)
58
+ if return_from_eval == original_question.eval_answer
59
+ true
60
+ else
61
+ puts "Remember, to call a method, you simply enter its name followed by any arguments it might need. Try again.\n".red
62
+ end
63
+ rescue NoMethodError => e
64
+ print_no_method_error_prompt
65
+ rescue NameError => e
66
+ print_name_error_prompt(e, user_submission)
67
+ rescue ArgumentError => e
68
+ print_argument_error_prompt(e)
69
+ end
70
+ end
71
+ end
72
+
73
+ end
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.78
4
+ version: 0.0.79
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Vander Hoop
@@ -70,14 +70,14 @@ dependencies:
70
70
  name: pry
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
@@ -109,6 +109,7 @@ files:
109
109
  - lib/battleroom/config/pry_config.rb
110
110
  - lib/battleroom/data/array_questions.rb
111
111
  - lib/battleroom/data/hash_questions.rb
112
+ - lib/battleroom/data/method_questions.rb
112
113
  - lib/battleroom/data/nested_data_structure_access_questions.rb
113
114
  - lib/battleroom/data/variable_assignment_questions.rb
114
115
  - lib/battleroom/data_generation_machinery.rb
@@ -120,6 +121,7 @@ files:
120
121
  - lib/battleroom/models/hash_access_question.rb
121
122
  - lib/battleroom/models/hash_assignment_question.rb
122
123
  - lib/battleroom/models/method_definition_question.rb
124
+ - lib/battleroom/models/method_invocation_question.rb
123
125
  - lib/battleroom/models/nested_data_structure_access_question.rb
124
126
  - lib/battleroom/models/question.rb
125
127
  - lib/battleroom/models/variable_question.rb