Puts to stream?

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.

···

--
Posted via http://www.ruby-forum.com/.

Maybe use a StringIO?

- -------------------------------
require "stringio"

str = StringIO.new("")
str.puts("hi")
str.puts("there")

puts str.string #=> "hi\nthere\n"
- -------------------------------

Vale,
Marvin

···

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.

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.
--
Posted via http://www.ruby-forum.com/\.

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

puts "hi"

hi
=> nil

require 'stringio'

=> true

my_out = StringIO.new('')

=> #<StringIO:0x1e5d20>

my_out.puts "hi"

=> nil

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:

begin

?> saved_stdout = $stdout

  $stdout = my_out
  puts "there"
ensure

?> $stdout = saved_stdout

end

=> nil

my_out.string

=> "hi\nthere\n"

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

-Rob

Rob Biedenharn
Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab@GaslightSoftware.com http://GaslightSoftware.com/

···

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

Sorry, this was meant to be

p str.string #=> "hi\nthere\n"

Vale,
Marvin

···

Am 03.10.2010 21:12, schrieb Quintus:

puts str.string #=> "hi\nthere\n"