On 3 February 2013 01:00, [email protected] wrote:
coloring and puts to stdout?
Philosophically I’d argue that this is more correct anyway. If your
intention is to change all Strings everywhere (stored in memory, written
files, sent over the network, stored in databases, etc.) to include
bytes that correspond with ANSI/VT escape sequences then by all means
monkeypatch String and co.; but if your intention is just to inject
bytes into console output, you should do the patching at the console
output phase, either by messing with the IO#print family of methods, or
adding the new #colored_puts method Marcus presented above.
Note, too, that it’s not just puts/print that call #to_s; if String#to_s
added ANSI escapes, you’d have to have special logic to ensure they were
only added once, or things could get a bit … out of hand.
When in doubt, look for a gem that already does what you want. Many
and some are even pretty good.
Matthew K., B.Sc (CompSci) (Hons)
“You’ll never find a programming language that frees
you from the burden of clarifying your ideas.” - xkcd