Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...