From: Matthew Kerwin Date: 2013-02-03T16:00:26+09:00 Subject: Re: Problem redefining String::to_s --047d7b2e4618c150d604d4cc871a Content-Type: text/plain; charset=ISO-8859-1 On 3 February 2013 01:00, wrote: > Am 02.02.2013 13:06, schrieb Javier 12: > >> >> it was an interesting find, and I have to rely on a custom String class >> because of that >> >> > Maybe you should describe your actual use case in more detail. > > Couldn't you simply use a wrapper method that does the > coloring and puts to stdout? > > Like: > > def colored_puts(string) > puts "\e[31m#{string}\e[0m" > end > > colored_puts 'Hi!' > > Philosophically I'd argue that this is more correct anyway. If your intention is to change all Strings everywhere (stored in memory, written to files, sent over the network, stored in databases, etc.) to include extra bytes that correspond with ANSI/VT escape sequences then by all means monkeypatch String and co.; but if your intention is just to inject those 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 exist, and some are even pretty good. ;) -- Matthew Kerwin, B.Sc (CompSci) (Hons) http://matthew.kerwin.net.au/ ABN: 59-013-727-651 "You'll never find a programming language that frees you from the burden of clarifying your ideas." - xkcd --047d7b2e4618c150d604d4cc871a Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On 3 February 2013 01:00, <sto.mar@web.de> = wrote:
Am 02.02.2013 13:06, schrieb Javier 12:

it was an interesting find, and I have to rely on a custom String class
because of that


Maybe you should describe your actual use case in more detail.

Couldn't you simply use a wrapper method that does the
coloring and puts to stdout?

Like:

=A0 def colored_puts(string)
=A0 =A0 puts "\e[31m#{string}\e[0m"
=A0 end

=A0 colored_puts 'Hi!'


<= div class=3D"gmail_extra" style>Philosophically I'd argue that this is = more correct anyway. =A0If your intention is to change all Strings everywhe= re (stored in memory, written to files, sent over the network, stored in da= tabases, etc.) to include extra bytes that correspond with ANSI/VT escape s= equences then by all means monkeypatch String and co.; but if your intentio= n is just to inject those bytes into _console output_, you should do the pa= tching at the _console output phase_, either by messing with the IO#print f= amily of methods, or adding the new #colored_puts method Marcus presented a= bove.

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 w= hat you want. =A0Many exist, and some are even pretty good. ;)

--
=A0 Matthew Kerwin, B.Sc (CompSci) (Hons)
=A0 http://matthew.kerwi= n.net.au/
=A0 ABN: 59-013-727-651

=A0 "You'll never find a programmin= g language that frees
=A0 you from the burden of clarifying your ideas.&= quot; - xkcd
--047d7b2e4618c150d604d4cc871a--