Skip to content

Instantly share code, notes, and snippets.

@shivabhusal
Created June 23, 2017 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shivabhusal/914a1017f3c86dfeee79a195a1e5abdf to your computer and use it in GitHub Desktop.
Save shivabhusal/914a1017f3c86dfeee79a195a1e5abdf to your computer and use it in GitHub Desktop.
Using Refinements to monkey patch String class
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