# How to overwrite standard puts method

**URL:** <https://www.ruby-forum.com/t/how-to-overwrite-standard-puts-method/197391>\
**Category:** Ruby\
**Created:** [October 13, 2010, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-overwrite-standard-puts-method/197391 "2010-10-13T19:30:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [October 13, 2010, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-overwrite-standard-puts-method/197391/1 "2010-10-13T19:30:45Z")

</div>

I’m pretty new to ruby.  
I’m using buildR to build our software.  
When buildR runs in the trace mode it prints out a huge amount of  
information to the standard output which is difficult to understand.

I would like to have a better control on the output format.  
I thought about overwriting standard puts method and use log4R in the  
new implementation to get better control on formatting/filtering.

Can it be done? Any other idea on how to get the better control on  
buildR output?

Thanks in advance,  
Arkady

---

<div class="post-metadata">

**Author:** ![arkady](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@arkady](https://www.ruby-forum.com/u/arkady)\
**Post date:** [October 13, 2010, 7:45pm UTC](https://www.ruby-forum.com/t/how-to-overwrite-standard-puts-method/197391/2 "2010-10-13T19:45:35Z")

</div>

On Thu, Oct 14, 2010 at 02:31:07AM +0900, Arkady Itkin wrote:

> buildR output?  
> Have you tried using sprintf to format output before printing it?

---

<div class="post-metadata">

**Author:** ![arkady](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@arkady](https://www.ruby-forum.com/u/arkady)\
**Post date:** [October 13, 2010, 7:49pm UTC](https://www.ruby-forum.com/t/how-to-overwrite-standard-puts-method/197391/3 "2010-10-13T19:49:57Z")

</div>

You can probably redirect all that output to a file with \>filename.txt  
and then look at it at your convenience.

---

<div class="post-metadata">

**Author:** ![arkady](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@arkady](https://www.ruby-forum.com/u/arkady)\
**Post date:** [October 13, 2010, 7:51pm UTC](https://www.ruby-forum.com/t/how-to-overwrite-standard-puts-method/197391/4 "2010-10-13T19:51:12Z")

</div>

On 10/13/2010 07:31 PM, Arkady Itkin wrote:

> buildR output?  
> There are several methods that write to stdout (off the top of my head  
> at least puts, print, printf). One option would be to assign to $stdout  
> another stream. If you want to catch child process output things get a  
> bit more complicated. In that case you either need to use IO#popen to  
> handle child process output or open a pipe and use $stdout.reopen to  
> attach the write end of the pipe to the stream. You then need to read  
> (asynchronously, i.e. in a thread) from the read end.

Btw, why is offline filtering not an option? I mean, if you write  
stdout to a file you can filter whatever you like and you still have the  
whole output.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![arkady](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@arkady](https://www.ruby-forum.com/u/arkady)\
**Post date:** [October 14, 2010, 10:08am UTC](https://www.ruby-forum.com/t/how-to-overwrite-standard-puts-method/197391/5 "2010-10-14T10:08:37Z")

</div>

Robert K. wrote in post #949915:

> On 10/13/2010 07:31 PM, Arkady Itkin wrote:
> 
> > buildR output?  
> > There are several methods that write to stdout (off the top of my head  
> > at least puts, print, printf). One option would be to assign to $stdout  
> > another stream. If you want to catch child process output things get a  
> > bit more complicated. In that case you either need to use IO#popen to  
> > handle child process output or open a pipe and use $stdout.reopen to  
> > attach the write end of the pipe to the stream. You then need to read  
> > (asynchronously, i.e. in a thread) from the read end.
> 
> Btw, why is offline filtering not an option? I mean, if you write  
> stdout to a file you can filter whatever you like and you still have the  
> whole output.

It looks like I did not explain my problem correctly.

I’m trying to debug a huge amount of buildR code which overflows me with  
tons of information printed out.  
Of course, I can redirect it to the file (both from within the code or  
from command prompt) and then try to filter out the needed information  
from the saved output. But since the output it’s not well  
organized/structured it’s not an easy task.

I thought about replacing all puts/print etc. calls that exists in the  
code by log4R which can be controlled from outside (and, on the way  
provide better structured output).  
Of course, I can find and replace puts/sprints etc. in the code to my  
method. But this is huge amount of code and I’m lazy :-).

So, I thought about some trick which will allow me to overwrite standard  
output methods like puts and get control of the output into my code  
where I can do whatever I want. It may be useful for other people as  
well when they need to debug the build code.

BTW, any other idea on how to debug buildR code will be appreciated. I  
tried to use ruby debugger with it but it fails in attempt to load  
buildR stuff.

Thanks in advance,  
Arkady

---

<div class="post-metadata">

**Author:** ![arkady](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@arkady](https://www.ruby-forum.com/u/arkady)\
**Post date:** [October 14, 2010, 5:52pm UTC](https://www.ruby-forum.com/t/how-to-overwrite-standard-puts-method/197391/6 "2010-10-14T17:52:24Z")

</div>

On 14.10.2010 10:08, Arkady Itkin wrote:

> > 
> 
> from the saved output. But since the output it’s not well  
> organized/structured it’s not an easy task.
> 
> I thought about replacing all puts/print etc. calls that exists in the  
> code by log4R which can be controlled from outside (and, on the way  
> provide better structured output).

What kind of structure do you expect to get? As far as I can see  
changing puts to internally delegate to a Logger can give you only  
timestamp and maybe thread id because puts itself only accepts message  
texts. You can especially _not_ get log levels with this approach.

> Of course, I can find and replace puts/sprints etc. in the code to my  
> method. But this is huge amount of code and I’m lazy :-).
> 
> So, I thought about some trick which will allow me to overwrite standard  
> output methods like puts and get control of the output into my code  
> where I can do whatever I want. It may be useful for other people as  
> well when they need to debug the build code.

Yes, of course you can do that. But I still wonder what kind of  
structure beyond added timestamps you expect.

> BTW, any other idea on how to debug buildR code will be appreciated. I  
> tried to use ruby debugger with it but it fails in attempt to load  
> buildR stuff.

Whenever I need to debug an ant build I use option -d (debug, really  
verbose), let all output flow into a file and look at it with my  
favorite text editor [1]. That supports setting bookmarks based on  
search patterns (POSIX regexp), you can copy all bookmarked lines into a  
new document etc.

Cheers

robert

[1] [http://www.textpad.com/](http://www.textpad.com/)

---

<div class="post-metadata">

**Author:** ![arkady](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@arkady](https://www.ruby-forum.com/u/arkady)\
**Post date:** [October 17, 2010, 8:57am UTC](https://www.ruby-forum.com/t/how-to-overwrite-standard-puts-method/197391/7 "2010-10-17T08:57:56Z")

</div>

Robert,

Thanks for the help.  
Please, see my comments inside.

Arkady

Robert K. wrote in post #950193:

> On 14.10.2010 10:08, Arkady Itkin wrote:
> 
> > > 
> > 
> > from the saved output. But since the output it’s not well  
> > organized/structured it’s not an easy task.
> > 
> > I thought about replacing all puts/print etc. calls that exists in the  
> > code by log4R which can be controlled from outside (and, on the way  
> > provide better structured output).
> 
> What kind of structure do you expect to get? As far as I can see  
> changing puts to internally delegate to a Logger can give you only  
> timestamp and maybe thread id because puts itself only accepts message  
> texts. You can especially _not_ get log levels with this approach.

I was thinking about getting information on caller of the “puts” method.  
That will allow me to print out who invoked the “puts” method and even  
print it with indentation reflecting calling stack.

> > Of course, I can find and replace puts/sprints etc. in the code to my  
> > method. But this is huge amount of code and I’m lazy :-).
> > 
> > So, I thought about some trick which will allow me to overwrite standard  
> > output methods like puts and get control of the output into my code  
> > where I can do whatever I want. It may be useful for other people as  
> > well when they need to debug the build code.
> 
> Yes, of course you can do that. But I still wonder what kind of  
> structure beyond added timestamps you expect.
> 
> > BTW, any other idea on how to debug buildR code will be appreciated. I  
> > tried to use ruby debugger with it but it fails in attempt to load  
> > buildR stuff.
> 
> Whenever I need to debug an ant build I use option -d (debug, really  
> verbose), let all output flow into a file and look at it with my  
> favorite text editor [1]. That supports setting bookmarks based on  
> search patterns (POSIX regexp), you can copy all bookmarked lines into a  
> new document etc.

Unfortunately buildR trace is not as useful as ant trace. For example,  
it does not print out the reason why it decides to trigger some task (at  
least I did not find it).  
Besides, contrary to ant, buildR is not pure declaration language but  
allows to write custom code which I want to debug. I wonder if I can use  
ruby debugger ti debug this code (meanwhile all my attempts failed, ruby  
debugger fails to load with builrR).

Thanks,

Arkady

> Cheers
> 
> robert
> 
> [1] [http://www.textpad.com/](http://www.textpad.com/)

---

<div class="post-metadata">

**Author:** ![arkady](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@arkady](https://www.ruby-forum.com/u/arkady)\
**Post date:** [October 17, 2010, 1:06pm UTC](https://www.ruby-forum.com/t/how-to-overwrite-standard-puts-method/197391/8 "2010-10-17T13:06:48Z")

</div>

On 17.10.2010 08:57, Arkady Itkin wrote:

> > What kind of structure do you expect to get? As far as I can see  
> > changing puts to internally delegate to a Logger can give you only  
> > timestamp and maybe thread id because puts itself only accepts message  
> > texts. You can especially _not_ get log levels with this approach.
> 
> I was thinking about getting information on caller of the “puts” method.  
> That will allow me to print out who invoked the “puts” method and even  
> print it with indentation reflecting calling stack.

You should then probably look into set\_trace\_func as another option.  
With that you can do custom logging of all methods invoked.

-------------------------------------------------- Kernel#set\_trace\_func  
set\_trace\_func(proc) =\> proc  
set\_trace\_func(nil) =\> nil

```
  From Ruby 1.9.1

```

* * *

```
  Establishes _proc_ as the handler for tracing, or disables tracing
  if the parameter is +nil+. _proc_ takes up to six parameters: an
  event name, a filename, a line number, an object id, a binding, 

```

and  
the name of a class. _proc_ is invoked whenever an event occurs.  
Events are: +c-call+ (call a C-language routine), +c-return+  
(return from a C-language routine), +call+ (call a Ruby method),  
+class+ (start a class or module definition), +end+ (finish a  
class  
or module definition), +line+ (execute code on a new line),  
+raise+  
(raise an exception), and +return+ (return from a Ruby method).  
Tracing is disabled within the context of _proc_.

```
      class Test
      def test
        a = 1
        b = 2
      end
      end

      set_trace_func proc { |event, file, line, id, binding, 

```

classname|  
printf “%8s %s:%-2d %10s %8s\n”, event, file, line, id,  
classname  
}  
t = Test.new  
t.test

```
        line prog.rb:11 false
      c-call prog.rb:11 new Class
      c-call prog.rb:11 initialize Object
    c-return prog.rb:11 initialize Object
    c-return prog.rb:11 new Class
        line prog.rb:12 false
        call prog.rb:2 test Test
        line prog.rb:3 test Test
        line prog.rb:4 test Test
      return prog.rb:4 test Test

```

You can try it out with a simple example like this:

ruby19 -e ‘set\_trace\_func lambda {|\*a| p a};5.times {|i| p i}’

> Unfortunately buildR trace is not as useful as ant trace. For example,  
> it does not print out the reason why it decides to trigger some task (at  
> least I did not find it).  
> Besides, contrary to ant, buildR is not pure declaration language but  
> allows to write custom code which I want to debug. I wonder if I can use  
> ruby debugger ti debug this code (meanwhile all my attempts failed, ruby  
> debugger fails to load with builrR).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![arkady](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@arkady](https://www.ruby-forum.com/u/arkady)\
**Post date:** [October 17, 2010, 2:06pm UTC](https://www.ruby-forum.com/t/how-to-overwrite-standard-puts-method/197391/9 "2010-10-17T14:06:36Z")

</div>

Robert,

Thanks a lot!  
set\_trace\_func is exactly what I was looking for.

Arkady
