From: sto.mar@... Date: 2013-02-01T15:24:28+09:00 Subject: Re: Problem redefining String::to_s Am 31.01.2013 19:56, schrieb Javier 12: > I've been checking String::to_s source code and found an issue. > > A program like this will not work: > > class String > def to_s > "example" > end > end > > puts "Hi" > > The output will be "Hi" and not "example". > > This is due to C code, it checks if object is a String, and then print > it directly without calling "to_s" (it calls to_s on all other classes) > > I wanted to override String::to_s to apply some color to my program > output (just to check some things) and I can't. > > Is this a missing feature? > > (i'm using ruby 1.8.7) (why???) The built-in classes behave sometimes differently than the classes you define. That's neither a bug nor a feature. Also, why should puts send to_s to a string? It *is* already a string. There seems to be gems to colorize output, or maybe this helps: http://stackoverflow.com/questions/1489183/colorized-ruby-output. class String def red "\e[31m#{self}\e[0m" end end puts 'Red text'.red Regards, Marcus --