Module: BattleroomMachinery

Defined in:
lib/battleroom/battleroom_machinery.rb

Constant Summary collapse

CONGRATULATIONS =
[
  'Lovely work, my friend!',
  'Beautiful!',
  'Lovely!',
  'Splendid!',
  'Awesome.',
  'Stand up job, my friend.',
  'Nailed it.',
  'Nice job! Keep it rolling!',
  'Capital work, battlestar!',
  'Exemplary work!',
  'Yeah!',
  'Roll on.',
  'Bullseye!',
  'Woo!',
  'Let it ride!',
  'Touchdown!',
  "You're on your way!",
  "You're making a prosperous go of this programming thing.",
]

Instance Method Summary collapse

Instance Method Details

#battleprint(string) ⇒ Object



66
67
68
# File 'lib/battleroom/battleroom_machinery.rb', line 66

def battleprint(string)
  puts dynamic_word_wrap(string)
end

#clear_displayObject



24
25
26
# File 'lib/battleroom/battleroom_machinery.rb', line 24

def clear_display
  `reset`
end

#determine_variable_follow_up_question(eval_scope, question) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/battleroom/battleroom_machinery.rb', line 94

def determine_variable_follow_up_question(eval_scope, question)
  if question.variable_value == true || question.variable_value == false
    VariableReassignmentQuestion.new(eval_scope, question)
  elsif question.variable_value.class == Symbol || question.variable_value.class == String
    nil
  else
    VariableReferenceQuestion.new(eval_scope, question)
  end
end

#dynamic_word_wrap(string) ⇒ Object



62
63
64
# File 'lib/battleroom/battleroom_machinery.rb', line 62

def dynamic_word_wrap(string)
  string.wrap(150)
end

#format_class_for_output(klass) ⇒ Object



137
138
139
140
# File 'lib/battleroom/battleroom_machinery.rb', line 137

def format_class_for_output(klass)
  return 'Boolean' if klass.to_s.match /(TrueClass|FalseClass)/
  klass.to_s
end

#format_value_for_stdout_and_eval(object) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/battleroom/battleroom_machinery.rb', line 114

def format_value_for_stdout_and_eval(object)
  case object.class.to_s
  when 'String'
    if object.include?("'")
      '"' + object + '"'
    else
      "'#{object}'"
    end
  when 'Symbol' then return ":#{object}"
  else
    object.to_s
  end
end

#indent_all_lines_for_stdout(string_to_indent) ⇒ Object



58
59
60
# File 'lib/battleroom/battleroom_machinery.rb', line 58

def indent_all_lines_for_stdout(string_to_indent)
  string_to_indent.gsub(/^.*/) { |match| "\t" + match }
end

#isolate_variable_name_from_name_error(error) ⇒ Object



70
71
72
# File 'lib/battleroom/battleroom_machinery.rb', line 70

def isolate_variable_name_from_name_error(error)
  /`(.+)'/i.match(error.message)
end

#naughty_input?(user_input) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
# File 'lib/battleroom/battleroom_machinery.rb', line 128

def naughty_input?(user_input)
  if user_input.match(/(require|`|binding)/)
    battleprint "No way no how! Try again.\n".red
    true
  else
    false
  end
end


51
52
53
54
55
56
# File 'lib/battleroom/battleroom_machinery.rb', line 51

def print_colorized_error_prompt(error)
  method_or_variable = (error.class == NoMethodError) ? "method" : "variable"
  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
  battleprint "\tundefined local variable or method \'WHATEVER_YOU_MISTYPED\'\n".green
  battleprint "Get used to it and try again.\n".red
end


104
105
106
107
108
109
110
111
112
# File 'lib/battleroom/battleroom_machinery.rb', line 104

def print_colorized_type_error_prompt(error)
  battleprint "\nNope! You just triggered a common Ruby error that reads:\n".red
  battleprint "\tin '[]', #{error.message}".green
  error.message.match /conversion\sof\s(.+)\sinto\sInteger/i
  battleprint "\nBasically, you put a #{$1} between square brackets, whereas Ruby ".red +
       "was expecting an index value, i.e. an integer. This commonly arises ".red +
       "when programmers think they're dealing with a hash, when in fact ".red +
       "they're dealing with an array. Try again.\n".red
end


42
43
44
# File 'lib/battleroom/battleroom_machinery.rb', line 42

def print_congratulation
  battleprint "\n#{random_congratulation}\n".green
end


28
29
30
31
32
33
34
35
# File 'lib/battleroom/battleroom_machinery.rb', line 28

def print_menu_options
  battleprint ' What would you like to work on?'.blue
  battleprint '1. Working with Variables'
  battleprint '2. Accessing Values in Arrays and Hashes'
  battleprint '3. Adding Values to Arrays and Hashes'
  # battleprint '4. Working with Methods'
  battleprint "Q. Quit\r\n\n"
end


84
85
86
87
88
89
90
91
92
# File 'lib/battleroom/battleroom_machinery.rb', line 84

def print_unexpected_end_of_input_explanation(error)
  battleprint "\nNope! You just triggered a common Ruby error that reads:\n".red
  battleprint "\tsyntax error, unexpected end-of-input\n".green
  if error.message.include?(']')
    battleprint "Basically, you used an opening square bracket '[', but didn't pair it with a closing square bracket. Try again.\n".red
  else
    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
  end
end

#random_congratulationObject



37
38
39
# File 'lib/battleroom/battleroom_machinery.rb', line 37

def random_congratulation
  CONGRATULATIONS.sample
end

#rotate_array(array) ⇒ Object



46
47
48
49
# File 'lib/battleroom/battleroom_machinery.rb', line 46

def rotate_array(array)
  item = array.shift
  array << item
end

#window_widthObject



74
75
76
77
78
79
80
81
82
# File 'lib/battleroom/battleroom_machinery.rb', line 74

def window_width
  begin
    width_string = `tput cols`
  # for cases where systems don't respond to the tput command
  rescue StandardError => e
    width_string = "80"
  end
  width_string.strip.to_i
end