Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting the results of pp (or anything outputted to console) into a string

Tags:

ruby

We know

require 'pp'
a=["value1", "value2", "value3"]
pp a

pretty prints the array as an output to the console. How do I get that pretty output into a string (a string containing the newlines that makes things pretty, etc.)?

...purpose being to return that pretty string from a method.

like image 738
user2015453 Avatar asked Mar 08 '13 00:03

user2015453


4 Answers

string_value = a.pretty_inspect

#pretty_inspect also comes along when you first require 'pp' - See: http://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/Kernel.html#method-i-pretty_inspect

If you want the version that is outputted to the irb console that is

 string_value = a.inspect

and doesn't have any requires necessary.

like image 155
rogerdpack Avatar answered Oct 20 '22 14:10

rogerdpack


This is a nice 'n simple way to capture the output of pp:

require 'pp'

asdf = {'a' => 1, :b => 2, 'c' => %w[ho daddy]}
foo = PP.pp(asdf, '')
puts foo
=> {"a"=>1, :b=>2, "c"=>["ho", "daddy"]}

Capturing STDOUT, which is the default channel used by puts and print and that things like pp piggyback on, is a bit more complex:

require 'pp'
require 'stringio'

asdf = {'a' => 1, :b => 2, 'c' => %w[ho daddy]}
puts 'Writing to STDOUT...'
pp asdf

# remember the old STDOUT stream...
old_stdout = $stdout

# ...and create a new stream that writes to a string.
captured_stdio = StringIO.new('', 'w')
$stdout = captured_stdio

# This is all captured...
puts 'Capturing to buffer...'
pp asdf

# reset STDOUT
$stdout = old_stdout
puts 'Capturing off...'

# show what we got...
puts captured_stdio.string

And what was printed:

Writing to STDOUT...
{"a"=>1, :b=>2, "c"=>["ho", "daddy"]}
Capturing off...
Capturing to buffer...
{"a"=>1, :b=>2, "c"=>["ho", "daddy"]}

The last two lines above were stored in captured_stdio by substituting that for the normal $stdout channel. Anything written to (what would be STDOUT) got stored. Swapping back in the original channel restored normal printing, and stopped anything else from being written to captured_stdio.

like image 28
the Tin Man Avatar answered Oct 20 '22 14:10

the Tin Man


Another way to use stringio, without changing $stdout:

require 'pp'
require 'stringio'

a=["value1", "value2", "value3"]

sio = StringIO.new
PP.pp( a, sio )

puts sio.string
like image 2
Uli Ramminger Avatar answered Oct 20 '22 14:10

Uli Ramminger


If you want to save the output into a string, you can use stringio

Here is an example:

#!/usr/bin/env ruby

require 'stringio'
require 'pp'

def output_to_string
  sio = StringIO.new
  old_stdout, $stdout = $stdout, sio
  yield
  $stdout = old_stdout # restore stdout
  sio.string
end

result = output_to_string do
  puts "hello"
  pp ["value1", "value2", "value3"]
end

puts "result: #{result}"

If you exec this code you get:

result: hello
["value1", "value2", "value3"]
like image 1
Sinetris Avatar answered Oct 20 '22 15:10

Sinetris