Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Outputting to a string

Reply
Thread Tools

Outputting to a string

 
 
Albert Schlef
Guest
Posts: n/a
 
      03-05-2010
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Dober
Guest
Posts: n/a
 
      03-05-2010
On Fri, Mar 5, 2010 at 6:47 AM, Albert Schlef <> wrot=
e:
> I have a function that generates some textual data that I capture into a
> string:
>
> =A0def gen_data(output, ...)
> =A0 =A0...
> =A0 =A0output << "... some data ..."
> =A0 =A0...
> =A0end
>
> =A0s =3D ''
> =A0gen_data(s, ...)
>
> I was wondering if instead it's possible to use a pattern similar to the
> following:
>
> =A0def gen_data(...)
> =A0 =A0...
> =A0 =A0puts "... some data ..."
> =A0 =A0...
> =A0end
>
> =A0s =3D capture_output do
> =A0 =A0gen_data(...)
> =A0end
>
> 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.
> --
> Posted via 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=3DStringIO::new
o=3D$stdout
$stdout=3Ds
blk[]
s.string
ensure
$stdout=3Do
end

HTH
R
--=20
Learning without thought is labor lost; thought without learning is perilou=
s.=94
--- Confucius

 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      03-05-2010
On Fri, Mar 5, 2010 at 6:47 AM, Albert Schlef <> wrot=
e:
> I have a function that generates some textual data that I capture into a
> string:
>
> =A0def gen_data(output, ...)
> =A0 =A0...
> =A0 =A0output << "... some data ..."
> =A0 =A0...
> =A0end
>
> =A0s =3D ''
> =A0gen_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 =3D "...some data..."
...
output
end

s =3D ""
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Outputting a string to STDOUT with our libc functions raashid bhatt C Programming 4 08-18-2008 06:51 PM
Two questions...first, outputting strings from a datafile, and string addition... snow.carriers@gmail.com C++ 3 03-02-2006 04:01 PM
Finding an outputting a string of text defined@comcast.net Ruby 0 01-11-2006 09:34 PM
Outputting in-line image from BLOB in ASP.NET Jonathan ASP .Net 4 12-15-2004 10:34 PM
outputting html through string variable Tom Javascript 1 11-16-2004 01:58 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57