本文介绍了将 pp 的结果(或输出到控制台的任何内容)放入字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道

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

pretty 将数组作为输出打印到控制台.如何将漂亮的输出转换为字符串(包含使事情变得漂亮的换行符等的字符串)?

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.

推荐答案

string_value = a.pretty_inspect

#pretty_inspect 在您第一次需要 'pp' 时也会出现 - 请参阅:http://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/Kernel.html#method-i-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

如果你想要输出到 irb 控制台的版本是

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.

这篇关于将 pp 的结果(或输出到控制台的任何内容)放入字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 05:34