Skip to content

Instantly share code, notes, and snippets.

@mangar
Created May 8, 2015 13:26
Show Gist options
  • Save mangar/000f741759ec288dd6be to your computer and use it in GitHub Desktop.
Save mangar/000f741759ec288dd6be to your computer and use it in GitHub Desktop.
Ruby Colorize Strings
#
#
#
class String
# colorization
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
# text
def black; colorize(30) end
def red; colorize(31) end
def green; colorize(32) end
def yellow; colorize(33) end
def blue; colorize(34) end
def magenta; colorize(35) end
def cyan; colorize(36) end
def gray; colorize(37) end
# bg
def bg_black; colorize(40) end
def bg_red; colorize(41) end
def bg_green; colorize(42) end
def bg_yellow; colorize(43) end
def bg_blue; colorize(44) end
def bg_magenta; colorize(45) end
def bg_cyan; colorize(46) end
def bg_gray; colorize(47) end
# extra
def bold; colorize(1) end
def reverse_color; colorize(7) end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment