Class: VariableQuestion
- Defined in:
- lib/battleroom/models/variable_question.rb
Instance Attribute Summary collapse
-
#formatted_value ⇒ Object
Returns the value of attribute formatted_value.
Attributes inherited from Question
#answer_value, #data, #data_structure, #data_structure_class, #evaluation_scope, #explanation, #type, #variable_name, #variable_value
Instance Method Summary collapse
- #evaluate_variable_assignment_input ⇒ Object
-
#initialize(eval_scope) ⇒ VariableQuestion
constructor
A new instance of VariableQuestion.
- #print_variable_assignment_prompt ⇒ Object
- #reveal_name_error_follies_to_user(user_input) ⇒ Object
Methods inherited from Question
#enter_evaluation_loop, generate_question
Constructor Details
#initialize(eval_scope) ⇒ VariableQuestion
Returns a new instance of VariableQuestion.
10 11 12 13 14 |
# File 'lib/battleroom/models/variable_question.rb', line 10 def initialize(eval_scope) super(eval_scope) @variable_value = rotate_array(data[:possible_variable_values]).first @formatted_value = format_value_for_stdout_and_eval(variable_value) end |
Instance Attribute Details
#formatted_value ⇒ Object
Returns the value of attribute formatted_value.
5 6 7 |
# File 'lib/battleroom/models/variable_question.rb', line 5 def formatted_value @formatted_value end |
Instance Method Details
#evaluate_variable_assignment_input ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/battleroom/models/variable_question.rb', line 41 def evaluate_variable_assignment_input enter_evaluation_loop do |user_input| begin evaluation_scope.eval(user_input) if evaluation_scope.eval("#{variable_name} == #{formatted_value}") # this last returned value of 'true' is vital; # the return value of yield is used in a conditional true else puts "You mis-assigned #{variable_name}. Try again!".red end rescue NameError => e reveal_name_error_follies_to_user(user_input) rescue Exception => e if e..match(/unterminated string/) puts 'Blurg! You neglected to provide closing quotes for your string. Try again!'.red else puts e. end end end end |
#print_variable_assignment_prompt ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/battleroom/models/variable_question.rb', line 16 def print_variable_assignment_prompt formatted_class = format_class_for_output(variable_value.class) substrings = [ 'Create a variable '.blue, variable_name.yellow, ' and assign it the '.blue, formatted_class.blue, ' value '.blue, formatted_value.yellow ] puts substrings.join end |
#reveal_name_error_follies_to_user(user_input) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/battleroom/models/variable_question.rb', line 29 def reveal_name_error_follies_to_user(user_input) if user_input.include?(variable_name) && !user_input.match(/[^=]=[^=]/i) puts 'You\'re not using the assignment operator!'.red elsif !user_input.match(/("|')/) && variable_value.class == String puts 'Rats! You\'ve just made a common rookie mistake! ' + 'Strings are always surrounded by quotes. Otherwise, Ruby will ' + 'think you\'re referencing a variable or method name. Try again.'.red else puts 'Looks like you mistyped the variable name. Check for misspellings and try again.'.red end end |