Puts to stream?

T

Terry Michaels

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.
 
Q

Quintus

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

Am 03.10.2010 20:54, schrieb Terry Michaels:
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.

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/

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-----
 
Q

Quintus

-----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/

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

Rob Biedenharn

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.

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 Biedenharn
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top