How to p to a string

Is there a way that I can cause p to store the output that it would have
sent to STDOUT into a string instead?

Thanks,
Carl Youngblood

Is there a way that I can cause p to store the output that it would have

sent to STDOUT into a string instead?

···

On Sat, 29 Nov 2003, Carl Youngblood wrote:

You could do something ugly like:
my_stringio = StringIO.new
$stdout = my_stringio
p “blah”
$stdout = STDOUT
puts my_stringio.string

The “p” method as defined sends output to $stdout (or, more accurately,
“rb_stdout” in C). So you have to redefine $stdout to redirect its
output.

Alternatively, you could redefine “p”.

Chad

Hi,
what about:

irb(main):001:0> p Range.new(1,2)
1…2
nil
irb(main):002:0> puts Range.new(1,2).inspect
1…2
nil

?

Best regards,
M.

···

–

Michal Safranek, email:

a=((“a”…“z”).to_a+["@","."]);p(("%b"%[0x645bbb83a6a496]
).scan(/…/).map{|x|a[Integer(“0b”+x)]}.join.reverse)

Carl Youngblood wrote:

Is there a way that I can cause p to store the output that it would have
sent to STDOUT into a string instead?

Doesn’t #inspect do what you want?

Joel VanderWerf wrote:

Carl Youngblood wrote:

Is there a way that I can cause p to store the output that it would
have sent to STDOUT into a string instead?

Doesn’t #inspect do what you want?

I think you’re right! I’m still in the process of learning Ruby. Here
a little and there little… Thanks.