Class: ColourOutput
- Inherits:
-
Object
- Object
- ColourOutput
- Defined in:
- lib/colour_output.rb
Instance Attribute Summary collapse
-
#colours ⇒ Object
Returns the value of attribute colours.
-
#default ⇒ Object
Returns the value of attribute default.
Instance Method Summary collapse
-
#default_colours ⇒ Object
default colors hash.
-
#initialize {|_self| ... } ⇒ ColourOutput
constructor
initialize and yield self in order to give a hook to modify class.
-
#method_missing(name, *args) ⇒ Object
if the method is missing we need to wrap the argument in a color from the method call.
-
#random(string) ⇒ Object
print out a random color.
Constructor Details
#initialize {|_self| ... } ⇒ ColourOutput
initialize and yield self in order to give a hook to modify class
5 6 7 8 9 10 |
# File 'lib/colour_output.rb', line 5 def initialize @colours = default_colours @default = "\033[92m" yield self if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
if the method is missing we need to wrap the argument in a color from the method call
26 27 28 29 30 31 32 |
# File 'lib/colour_output.rb', line 26 def method_missing(name, *args) if @colours.include? name "#{@colours[name]}#{args[0]}\033[0m" else "#{@default}#{args[0]}\033[0m" end end |
Instance Attribute Details
#colours ⇒ Object
Returns the value of attribute colours.
2 3 4 |
# File 'lib/colour_output.rb', line 2 def colours @colours end |
#default ⇒ Object
Returns the value of attribute default.
2 3 4 |
# File 'lib/colour_output.rb', line 2 def default @default end |
Instance Method Details
#default_colours ⇒ Object
default colors hash
13 14 15 |
# File 'lib/colour_output.rb', line 13 def default_colours {primary: "\033[95m", blue: "\033[94m", green: "\033[92m", warning: "\033[93m", } end |
#random(string) ⇒ Object
print out a random color
18 19 20 21 22 |
# File 'lib/colour_output.rb', line 18 def random(string) random_key = @colours.keys.sample self.send(random_key, string) end |