# Outputting to a string

**URL:** <https://www.ruby-forum.com/t/outputting-to-a-string/184612>\
**Category:** Ruby\
**Created:** [March 5, 2010, 6:47am UTC](https://www.ruby-forum.com/t/outputting-to-a-string/184612 "2010-03-05T06:47:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [March 5, 2010, 6:47am UTC](https://www.ruby-forum.com/t/outputting-to-a-string/184612/1 "2010-03-05T06:47:26Z")

</div>

I have a function that generates some textual data that I capture into a  
string:

def gen\_data(output, …)  
…  
output \<\< “… some data …”  
…  
end

s = ‘’  
gen\_data(s, …)

I was wondering if instead it’s possible to use a pattern similar to the  
following:

def gen\_data(…)  
…  
puts “… some data …”  
…  
end

s = capture\_output do  
gen\_data(…)  
end

That’s because I want to pass gen\_data() as few arguments as possible  
(to make the code look “clean”. But also because my current code already  
uses ‘puts’. And also because I encountered this need several times in  
the past and I’m curious.)

I notice that this last pattern has a problem: if some debugging code  
uses ‘puts’ too, the output will be ruined. So I’m interested to hears  
about similar patterns and not necessarily the faulty one I devised  
here.

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [March 5, 2010, 8:51am UTC](https://www.ruby-forum.com/t/outputting-to-a-string/184612/2 "2010-03-05T08:51:16Z")

</div>

On Fri, Mar 5, 2010 at 6:47 AM, Albert S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## gen\_data(s, …) s = capture\_output do about similar patterns and not necessarily the faulty one I devised here.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

very ugly, but I am sure you can c?lean it up  
require ‘stringio’

def gen\_data; puts “42, I believe” end

def with\_stdout &blk  
s=StringIO::new  
o=$stdout  
$stdout=s  
blk[]  
s.string  
ensure  
$stdout=o  
end

HTH  
R

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [March 5, 2010, 9:19am UTC](https://www.ruby-forum.com/t/outputting-to-a-string/184612/3 "2010-03-05T09:19:22Z")

</div>

On Fri, Mar 5, 2010 at 6:47 AM, Albert S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> gen\_data(s, …)  
> I’d just have gen\_data return the string and let the caller make the  
> append to the string. What you have looks like an output argument and  
> that’s what the return value of the method should be used for, if  
> possible:

def gen\_data  
…  
output = “…some data…”  
…  
output  
end

s = “”  
s \<\< gen\_data

I suppose there’s a way to redirect where puts writes into a String as  
Robert has shown, but that could have some problems, as you stated: if  
some other code inside uses puts, it will get appended to the string.  
So, if that is exactly your use case (redirect all stdout to a string  
for a method call) that could work. But I’d refactor the method, if  
all you want to do is to return a string to the caller.

Jesus.
