Class: ColourOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/colour_output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ ColourOutput

initialize and yield self in order to give a hook to modify class

Yields:

  • (_self)

Yield Parameters:

  • _self (ColourOutput)

    the object that the method was called on



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

#coloursObject

Returns the value of attribute colours.



2
3
4
# File 'lib/colour_output.rb', line 2

def colours
  @colours
end

#defaultObject

Returns the value of attribute default.



2
3
4
# File 'lib/colour_output.rb', line 2

def default
  @default
end

Instance Method Details

#default_coloursObject

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