Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There are several methods for utilizing Ruby to add colored text to the terminal output. One common way is to use ANSI escape sequences. Here's an example:

puts "\e[31mThis text will be red\e[0m"

In this code, the "\e" character represents the escape key, followed by the code for the specific color (in this case, 31 for red), and ending with a reset code "\e[0m" to return the text to its default color.

Other colors can be specified using different codes, such as 32 for green, 33 for yellow, and so on. Different effects can also be achieved using additional codes, such as bold, underline, and blink.

Ruby also offers several libraries and gems for more advanced terminal formatting, such as the "colored" gem or the "tty-color" gem. These libraries provide additional methods and features for controlling the text color, background color, and text effects in the terminal output.