Class: Question
- Inherits:
-
Object
- Object
- Question
- Defined in:
- lib/battleroom/models/question.rb
Direct Known Subclasses
DataStructureQuestion, FollowUpQuestion, MethodDefinitionQuestion, VariableAssignmentQuestion
Instance Attribute Summary collapse
-
#answer_value ⇒ Object
Returns the value of attribute answer_value.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#data_structure ⇒ Object
Returns the value of attribute data_structure.
-
#data_structure_class ⇒ Object
Returns the value of attribute data_structure_class.
-
#evaluation_scope ⇒ Object
Returns the value of attribute evaluation_scope.
-
#explanation ⇒ Object
Returns the value of attribute explanation.
-
#input_mechanism ⇒ Object
Returns the value of attribute input_mechanism.
-
#user_input ⇒ Object
Returns the value of attribute user_input.
-
#variable_name ⇒ Object
Returns the value of attribute variable_name.
-
#variable_value ⇒ Object
Returns the value of attribute variable_value.
Class Method Summary collapse
-
.generate_question ⇒ Object
retrieves question from front of the array and rotates it to the back to avoid immediate re-sampling of questions.
Instance Method Summary collapse
- #congratulation_sequence(duration) ⇒ Object
- #enter_evaluation_loop(&block) ⇒ Object
- #get_input ⇒ Object
- #handle_syntax_error_exceptions(error) ⇒ Object
-
#initialize(evaluation_scope) ⇒ Question
constructor
A new instance of Question.
Constructor Details
#initialize(evaluation_scope) ⇒ Question
7 8 9 10 11 12 |
# File 'lib/battleroom/models/question.rb', line 7 def initialize(evaluation_scope) @evaluation_scope = evaluation_scope @data = self.class.generate_question @variable_name = rotate_array(data[:possible_variable_names] || []).first @input_mechanism = 'readline' end |
Instance Attribute Details
#answer_value ⇒ Object
Returns the value of attribute answer_value.
3 4 5 |
# File 'lib/battleroom/models/question.rb', line 3 def answer_value @answer_value end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
2 3 4 |
# File 'lib/battleroom/models/question.rb', line 2 def data @data end |
#data_structure ⇒ Object
Returns the value of attribute data_structure.
3 4 5 |
# File 'lib/battleroom/models/question.rb', line 3 def data_structure @data_structure end |
#data_structure_class ⇒ Object
Returns the value of attribute data_structure_class.
3 4 5 |
# File 'lib/battleroom/models/question.rb', line 3 def data_structure_class @data_structure_class end |
#evaluation_scope ⇒ Object
Returns the value of attribute evaluation_scope.
3 4 5 |
# File 'lib/battleroom/models/question.rb', line 3 def evaluation_scope @evaluation_scope end |
#explanation ⇒ Object
Returns the value of attribute explanation.
3 4 5 |
# File 'lib/battleroom/models/question.rb', line 3 def explanation @explanation end |
#input_mechanism ⇒ Object
Returns the value of attribute input_mechanism.
3 4 5 |
# File 'lib/battleroom/models/question.rb', line 3 def input_mechanism @input_mechanism end |
#user_input ⇒ Object
Returns the value of attribute user_input.
3 4 5 |
# File 'lib/battleroom/models/question.rb', line 3 def user_input @user_input end |
#variable_name ⇒ Object
Returns the value of attribute variable_name.
3 4 5 |
# File 'lib/battleroom/models/question.rb', line 3 def variable_name @variable_name end |
#variable_value ⇒ Object
Returns the value of attribute variable_value.
3 4 5 |
# File 'lib/battleroom/models/question.rb', line 3 def variable_value @variable_value end |
Class Method Details
.generate_question ⇒ Object
retrieves question from front of the array and rotates it to the back to avoid immediate re-sampling of questions
16 17 18 19 20 21 |
# File 'lib/battleroom/models/question.rb', line 16 def self.generate_question # calls upon *class instance* variable assigned in the subclasses question = @questions.shift @questions.push(question) question end |
Instance Method Details
#congratulation_sequence(duration) ⇒ Object
23 24 25 26 27 |
# File 'lib/battleroom/models/question.rb', line 23 def congratulation_sequence(duration) print_congratulation sleep(duration) clear_display end |
#enter_evaluation_loop(&block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/battleroom/models/question.rb', line 46 def enter_evaluation_loop(&block) answered_correctly = false until answered_correctly begin user_input = get_input abort('Goodbye!'.green) if user_input.match(/^(q|exit|!!!\s?)\z/i) if !naughty_input?(user_input) && yield(user_input) congratulation_sequence(1.6) answered_correctly = true end rescue SyntaxError => e handle_syntax_error_exceptions(e) end end end |
#get_input ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/battleroom/models/question.rb', line 37 def get_input if input_mechanism == 'readline' Readline.readline('> '.blue, true).chomp else Pry.start_without_pry_byebug(evaluation_scope) $input.chomp end end |
#handle_syntax_error_exceptions(error) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/battleroom/models/question.rb', line 29 def handle_syntax_error_exceptions(error) if error..match /unexpected end-of-input/ print_unexpected_end_of_input_explanation(error) elsif error..include?('unterminated string meets end of file') battleprint 'Blurg! You neglected to provide closing quotes for your string. Try again!'.red end end |