# Ruby has great gems for coloring print to terminal,
# but sometimes you just need to print a couple of times and
# don't want to add another gem just for that.
# For example, instead of:
puts "\e[31mThis is red!\e[0m"
puts "\e[32mThis is green!\e[0m"
# create the format separated on variables, using %s placeholder (printf format)
red = "\e[31m%s\e[0m"
green = "\e[32m%s\e[0m"
# and then use the "%" operator for readable formatting:
puts red % 'Red Again!'
puts green % 'Green Again!'
puts 'Mixed %s with %s!' % [red % 'red', green % 'green']
# Other example:
a = 2
b = 3
sum = a + b
format = '%s + %s = %s'
puts format % [red % a, red % b, green % sum]
# instead of the unreadable code:
puts "\e[31m#{a}\e[0m + \e[31m#{b}\e[0m = \e[32m#{sum}\e[0m"
# This is a simple, and unobtrusive to be used in a single place, like a log method:
class MyClass
def log(message, level)
red = "\e[31m%s\e[0m"
green = "\e[32m%s\e[0m"
puts "%s: %s" % [red % level, green % message]
end
end
# ----------------------------------------------------------------------------------------------------------------------
# If you want to use globally, you can monkey patch String class:
class String
def red
"\e[31m#{self}\e[0m"
end
def green
"\e[32m#{self}\e[0m"
end
end
# and then use them!
puts "Red again!".red
puts "Green again!".green
puts "Mixed %s with %s!" % ["red".red, "green".green]
# Same example above:
a = 2
b = 3
format = "%s + %s = %s"
puts format % [a.to_s.red, b.to_s.red, (a + b).to_s.green]
# ----------------------------------------------------------------------------------------------------------------------
# Or be more polite while monkey patching, using a module:
module ConsoleColors
def red
"\e[31m#{self}\e[0m"
end
def green
"\e[32m#{self}\e[0m"
end
end
String.include ConsoleColors
# ----------------------------------------------------------------------------------------------------------------------
# If you still want to use globally but don't want to monkey patch String, try this:
class Color
# constants
RED = "\e[31m%s\e[0m"
GREEN = "\e[32m%s\e[0m"
# or methods
def self.red(s)
"\e[31m#{s}\e[0m"
end
def self.green(s)
"\e[32m#{s}\e[0m"
end
end
puts Color::RED % 'reddish'
puts Color::GREEN % 'greenish'
puts Color.red 'reddish method'
puts Color.green 'greenish method'
# ----------------------------------------------------------------------------------------------------------------------
# Or use Ruby 2.0 refinements to limit the scope of the change
module ConsoleColorsStringRefinement
refine String do
def red
"\e[31m#{self}\e[0m"
end
def green
"\e[32m#{self}\e[0m"
end
end
end
class MyLogClass
using ConsoleColorsStringRefinement
def log(message, level)
puts "%s: %s" % [level.to_s.red, message.to_s.green]
end
end
MyLogClass.new.log('My Message', 'DEBUG') # DEBUG: My Message