# Capturing stdout from so library

**URL:** <https://rubytalk.org/t/capturing-stdout-from-so-library/13430>\
**Category:** ruby-talk\
**Created:** [5 September 2004 17:50 UTC](https://rubytalk.org/t/capturing-stdout-from-so-library/13430 "2004-09-05T17:50:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bjorn\_Nordbo](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@Bjorn\_Nordbo](https://rubytalk.org/u/Bjorn_Nordbo)\
**Post date:** [5 September 2004 17:50 UTC](https://rubytalk.org/t/capturing-stdout-from-so-library/13430/1 "2004-09-05T17:50:13Z")

</div>

I have a package (rrdtool) that provides functionality through a shared  
library and a Ruby interface (ruby-rrd) to it.

The problem is that one of the functions in the library (rrd\_graph) only  
returns data through a file descriptor; a file or standard output.

This is rather dumb in my opinion, but probably hard to fix. Also, a  
a workaround seems easy:

class StringBuffer  
&nbsp;&nbsp;attr\_reader :buffer  
&nbsp;&nbsp;  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@buffer = String.new  
&nbsp;&nbsp;end

&nbsp;&nbsp;def write(string)  
&nbsp;&nbsp;&nbsp;&nbsp;@buffer \<\< string  
&nbsp;&nbsp;end  
end

begin  
&nbsp;&nbsp;oldout = $stdout  
&nbsp;&nbsp;$stdout = StringBuffer.new  
&nbsp;&nbsp;RRD.graph(... tons of options ...)  
&nbsp;&nbsp;image = $stdout.buffer  
&nbsp;&nbsp;$stdout = oldout  
end

According to my theory, the StringBuffer object should have captured all  
data sent to standard output while $stdout points to it, and image sould  
contain a beautiful graph produced by RRD.graph.

However, what happens is that RRD.graph blatantly ignores the fact that  
$stdout is redirected, and bangs its data to standard output anyway.

I assume this means that there's nothing I can do in Ruby to capture the  
output to standard out from a library written in C. If this is true,  
I have two options:

- Do something (but what?) in the Ruby/C interface  
- Fix the library

Please tell me I'm wrong. 🙂

> **···**
>
> --  
> Bjørn
