# Puts to stream?

**URL:** <https://www.ruby-forum.com/t/puts-to-stream/195950>\
**Category:** Ruby\
**Created:** [October 3, 2010, 8:54pm UTC](https://www.ruby-forum.com/t/puts-to-stream/195950 "2010-10-03T20:54:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [October 3, 2010, 8:54pm UTC](https://www.ruby-forum.com/t/puts-to-stream/195950/1 "2010-10-03T20:54:43Z")

</div>

I’ve got simple statements like so:

puts “hi”  
puts “there”  
puts name

Wanted to adjust it to write to IO object (a string) instead of stdout.  
This is the kind of construction I’d like to use:

my\_out = String.new  
puts my\_out “hi”  
puts my\_out “there”  
puts my\_out name

But apparently that doesn’t work. This seems to work

my\_out \<\< “hi” \<\< “\n”  
my\_out \<\< “there” \<\< “\n”  
my\_out \<\< name \<\< “\n”

The lame part of that, though, is that it does not do the whole “check  
if there is an end-line and add one if there isn’t” magic, so I have to  
manually specify the newline if I want one. I tried

my\_out.puts “hi”

But evidently that does the exact opposite of want I want.

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [October 3, 2010, 9:13pm UTC](https://www.ruby-forum.com/t/puts-to-stream/195950/2 "2010-10-03T21:13:16Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Am 03.10.2010 20:54, schrieb Terry M.:

> puts my\_out “hi”  
> if there is an end-line and add one if there isn’t" magic, so I have to  
> manually specify the newline if I want one. I tried
> 
> my\_out.puts “hi”
> 
> But evidently that does the exact opposite of want I want.

Maybe use a StringIO?

* * *

require “stringio”

str = StringIO.new(“”)  
str.puts(“hi”)  
str.puts(“there”)

puts str.string #=\> “hi\nthere\n”

* * *

Vale,  
Marvin  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.10 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org/](http://enigmail.mozdev.org/)

iQEcBAEBAgAGBQJMqNWWAAoJEGrS0YjAWTKVqRwH/1+fbPjc756bpCJr2T6dRtas  
I83rVh/CLmEQkmnCq+S8Lg7yJNay+vAyhxJo0nACfNk8pUfPcAiZI/oaTsb/7Edx  
d7bLweNwuC9u2J2s2s8RB3tiQc1zkZRVrzZb8vzTBX6DX/h5XJXnI+SDM/RmPr9E  
DwHaKNj+4Pr3DPS6DCfxkmznTIsEFUodNT76KhsoUlR+dwbPCC8PMxhcSqRoM0no  
mpctqCDqfWbw0981ebtIJgixp1bMMaSRHzatk+H2HnMtLDkdx4v/HInQ67xT+BkK  
x7Z/xooAPs916KQ/xMX2l+lE4hYnCqFsW0t3mCDrYN+T7mS0dySHfqeBXRKeuqI=  
=WRu4  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [October 3, 2010, 9:15pm UTC](https://www.ruby-forum.com/t/puts-to-stream/195950/3 "2010-10-03T21:15:01Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Am 03.10.2010 21:12, schrieb Quintus:

> puts str.string #=\> “hi\nthere\n”

Sorry, this was meant to be

p str.string #=\> “hi\nthere\n”

Vale,  
Marvin  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.10 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org/](http://enigmail.mozdev.org/)

iQEcBAEBAgAGBQJMqNYaAAoJEGrS0YjAWTKV7fUH/30vvlU3Aqksx+dSlqUTE0cD  
meSFdJZUmTLQu/TpkzdAIctiMwX/Xs6bodnC+c4TU5yxiY312sZmYf7SOMREllL+  
jXCTX2qM4nK111Ptfr2F+m0F5I79b3pQhp/Fi1NUUGyUoE4H4yu6zy2QzhM6uBo1  
VMbBaIjfGqRMmQDiwFgzPSexmp8Ms5pQUZnNJHHg9vyySqnbruZMbrbedJjAScA5  
ft2vVn0G1VXzdUO54QAgHa5AxY2/Mir4BrNB/mBZyAHIChjYVbD2tjNm9+M/0GfQ  
8crJM80AdOfS1egUVkod2uI5UJtwrJnC3PlldQc5GtRGEqloYrChQuPbV/HpVwA=  
=+Uuk  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [October 3, 2010, 9:20pm UTC](https://www.ruby-forum.com/t/puts-to-stream/195950/4 "2010-10-03T21:20:17Z")

</div>

On Oct 3, 2010, at 2:54 PM, Terry M. wrote:

> my\_out = String.new  
> The lame part of that, though, is that it does not do the whole “check  
> if there is an end-line and add one if there isn’t” magic, so I have  
> to  
> manually specify the newline if I want one. I tried
> 
> my\_out.puts “hi”
> 
> ## But evidently that does the exact opposite of want I want.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

In addition to Quintus, here’s a bit more depending on how far you’re  
looking to go:

irb\> puts “hi”  
hi  
=\> nil  
irb\> require ‘stringio’  
=\> true  
irb\> my\_out = StringIO.new(‘’)  
=\> #StringIO:0x1e5d20  
irb\> my\_out.puts “hi”  
=\> nil  
irb\> my\_out.string  
=\> “hi\n”

So you can explicitly use puts on the StringIO instance.

If you want to implicitly target your StringIO, you can:

irb\> begin  
?\> saved\_stdout = $stdout  
irb\> $stdout = my\_out  
irb\> puts “there”  
irb\> ensure  
?\> $stdout = saved\_stdout  
irb\> end  
=\> nil  
irb\> my\_out.string  
=\> “hi\nthere\n”

Note that the ensure block puts $stdout back to where it started.

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)
