On Fri, Mar 5, 2010 at 6:47 AM, Albert Schlef <albertschlef / gmail.com> wrote:
> I have a function that generates some textual data that I capture into a
> string:
>
>  ¨Βεζ ηεξίδατα¨ουτπυτ¬ ®®®©
> ..
>  ¨ΒυτπυΌΌ Ά®®® σονε δατα ®®®Ά
> ..
>  ¨Βξδ
>
>  ¨Β §§
>  ¨Βεξίδατα¨σ¬ ®®®©
>
> I was wondering if instead it's possible to use a pattern similar to the
> following:
>
>  ¨Βεζ ηεξίδατᨮ®®> ..
>  ¨ΒυτΆ®®® σονε δατα ®®®Ά
> ..
>  ¨Βξδ
>
>  ¨Β γαπτυςείουτπυτ δο
>  ¨Βεξίδατᨮ®®©
>  ¨Βξδ
>
> 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=StringIO::new
  o=$stdout
  $stdout=s
  blk[]
  s.string
ensure
  $stdout=o
end

HTH
R
-- 
Learning without thought is labor lost; thought without learning is perilous.--- Confucius