Module: BattleroomMachinery

Defined in:
lib/battleroom/battleroom_machinery.rb

Constant Summary collapse

CONGRATULATIONS =
[
  'Lovely work, my friend!',
  'Beautiful!',
  'Lovely!',
  'Splendid!',
  '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

#clear_displayObject



21
22
23
# File 'lib/battleroom/battleroom_machinery.rb', line 21

def clear_display
  `reset`
end

#format_class_for_output(klass) ⇒ Object



93
94
95
96
# File 'lib/battleroom/battleroom_machinery.rb', line 93

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



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

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

#naughty_input?(user_input) ⇒ Boolean

Returns:

  • (Boolean)


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

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


47
48
49
50
51
52
# File 'lib/battleroom/battleroom_machinery.rb', line 47

def print_colorized_error_prompt(error)
  method_or_variable = (error.class == NoMethodError) ? "method" : "variable"
  puts "\nYou're referencing a #{method_or_variable} that doesn't exist, probably as the result of a mispelling. This results in a common error that says: \n".red
  puts "\tundefined local variable or method \'WHATEVER_YOU_MISTYPED\'\n".green
  puts "Get used to it and try again.\n".red
end


60
61
62
63
64
65
66
67
68
# File 'lib/battleroom/battleroom_machinery.rb', line 60

def print_colorized_type_error_prompt(error)
  puts "\nNope! You just triggered a common Ruby error that reads:\n".red
  puts "\tin '[]', #{error.message}".green
  error.message.match /conversion\sof\s(.+)\sinto\sInteger/i
  puts "\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


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

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


25
26
27
28
29
30
31
32
# File 'lib/battleroom/battleroom_machinery.rb', line 25

def print_menu_options
  puts ' What would you like to work on?'.blue
  puts '1. Variable assignment'
  puts '2. Accessing values in arrays and hashes'
  puts '3. Adding values to arrays and hashes'
  puts '4. Accessing values from within nested data structures'
  puts "Q. Quit\r\n\n"
end


54
55
56
57
58
# File 'lib/battleroom/battleroom_machinery.rb', line 54

def print_unexpected_end_of_input_explanation
  puts "\nNope! You just triggered a common Ruby error that reads:\n".red
  puts "\tsyntax error, unexpected end-of-input\n".green
  puts "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

#random_congratulationObject



34
35
36
# File 'lib/battleroom/battleroom_machinery.rb', line 34

def random_congratulation
  CONGRATULATIONS.sample
end

#rotate_array(array) ⇒ Object



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

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