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

前端 未结 4 584
心在旅途
心在旅途 2020-12-09 15:00

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 p

4条回答
  •  一向
    一向 (楼主)
    2020-12-09 15:40

    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.

提交回复
热议问题