Created
June 23, 2017 05:39
-
-
Save shivabhusal/914a1017f3c86dfeee79a195a1e5abdf to your computer and use it in GitHub Desktop.
Using Refinements to monkey patch String class
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
module StringColorize | |
refine String do | |
def red | |
color_code = 31 | |
"\e[#{color_code}m#{self}\e[0m" | |
end | |
def blue | |
color_code = 34 | |
"\e[#{color_code}m#{self}\e[0m" | |
end | |
def pink | |
color_code = 35 | |
"\e[#{color_code}m#{self}\e[0m" | |
end | |
end | |
end | |
# color methods wont be available here | |
require "pry"; binding.pry | |
#puts "This should be red".red | |
using StringColorize | |
# now the colorize methods will be available | |
puts "This should be red".red | |
puts "This should be red".blue | |
require "pry"; binding.pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment