# How to capture standart output to variable?

**URL:** <https://www.ruby-forum.com/t/how-to-capture-standart-output-to-variable/159785>\
**Category:** Ruby\
**Created:** [February 8, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/how-to-capture-standart-output-to-variable/159785 "2009-02-08T21:08:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkdwv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@rkdwv](https://www.ruby-forum.com/u/rkdwv)\
**Post date:** [February 8, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/how-to-capture-standart-output-to-variable/159785/1 "2009-02-08T21:08:49Z")

</div>

How to capture output of this code to variable to send it to iconv?  
(Just like ob\_start() in PHP)

# ???

p [“utf-8 string”,[123,321],“another string”]

# ???

puts Iconv.conv(“866”,“UTF-8”,captured\_output) # because of windows  
console

---

<div class="post-metadata">

**Author:** ![rkdwv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@rkdwv](https://www.ruby-forum.com/u/rkdwv)\
**Post date:** [February 8, 2009, 10:48pm UTC](https://www.ruby-forum.com/t/how-to-capture-standart-output-to-variable/159785/2 "2009-02-08T22:48:07Z")

</div>

Maxim Zhukov wrote:

> How to capture output of this code to variable to send it to iconv?  
> (Just like ob\_start() in PHP)
> 
> # ???
> 
> p [“utf-8 string”,[123,321],“another string”]
> 
> # ???
> 
> puts Iconv.conv(“866”,“UTF-8”,captured\_output) # because of windows  
> console

This irb example may help:  
±---------------------------------------------+  
| \>\> pipe = IO.popen(“echo ‘drop him a line’”) |  
| =\> #IO:0xb7a7b450 |  
| \>\> capture = pipe.read |  
| =\> “drop him a line\n” |  
| \>\> puts capture |  
| drop him a line |  
| =\> nil |  
| \>\> |  
±---------------------------------------------+

---

<div class="post-metadata">

**Author:** ![rkdwv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@rkdwv](https://www.ruby-forum.com/u/rkdwv)\
**Post date:** [February 8, 2009, 11:13pm UTC](https://www.ruby-forum.com/t/how-to-capture-standart-output-to-variable/159785/3 "2009-02-08T23:13:49Z")

</div>

Maxim Zhukov wrote:

> How to capture output of this code to variable to send it to iconv?  
> (Just like ob\_start() in PHP)
> 
> # ???
> 
> p [“utf-8 string”,[123,321],“another string”]
> 
> # ???
> 
> puts Iconv.conv(“866”,“UTF-8”,captured\_output) # because of windows  
> console

You can send output to a string:

require “stringio”

strio = StringIO.new  
strio.write(“hello world”)  
strio.rewind()  
puts strio.read()

old\_out = $stdout  
$stdout = strio

puts " goodbye"  
$stdout = old\_out

strio.rewind()  
puts strio.read()

–output:–  
hello world  
hello world goodbye

---

<div class="post-metadata">

**Author:** ![rkdwv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@rkdwv](https://www.ruby-forum.com/u/rkdwv)\
**Post date:** [February 9, 2009, 11:00pm UTC](https://www.ruby-forum.com/t/how-to-capture-standart-output-to-variable/159785/4 "2009-02-09T23:00:53Z")

</div>

7stud – wrote:

> You can send output to a string:
> 
> require “stringio”  
> . . .

Do this with “stringio” 😉

±----------------------------------------------+  
| \>\> pipe = IO.popen("/usr/games/fortune") |  
| =\> #IO:0xb7a6f588 |  
| \>\> capture = pipe.read |  
| =\> “You have taken yourself too seriously.\n” |  
| \>\> puts capture |  
| You have taken yourself too seriously. |  
| =\> nil |  
| \>\> |  
±----------------------------------------------+
