Last active
June 6, 2023 16:21
-
-
Save Lockyy/1357ea7f307f1fecc4da1c3a97a1151d to your computer and use it in GitHub Desktop.
Monkey patch to add `.red`/`.green`/`.yellow` to the String class to color strings on console output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def colorize(color_code) | |
"\e[#{color_code}m#{self}\e[0m" | |
end | |
def red | |
colorize(31) | |
end | |
def green | |
colorize(32) | |
end | |
def yellow | |
colorize(33) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/1489183/colorized-ruby-output