pretty print to a file in ruby

后端 未结 7 753
甜味超标
甜味超标 2021-02-07 00:11

I am trying to pretty print a hash to a file.

I tried unix redirects [added different flags to it incrementally] :

`echo #{pp  mymap} | tee summary.out          


        
相关标签:
7条回答
  • 2021-02-07 00:41

    Given the mymap from the question, you can use the "show_data" gem, which provides two methods: show_data and format_data. The latter produces a "pretty-printed" string of its argument which can then be fed to any output method.

    require 'show_data'
    
    $stderr.puts format_data(mymap)
    

    For example:

    myhash = { 'owners' => 21050, 'users' => 16877, 'portfolios' => 583, 
               'properylists' => 0, 'properties' => 29504, 'units' => 62688, 
               'tenants' => 85856 }
    $stderr.puts format_data(myhash)
    

    on STDERR:

     {      'owners' => 21050,
             'users' => 16877,
        'portfolios' => 583,
      'properylists' => 0,
        'properties' => 29504,
             'units' => 62688,
           'tenants' => 85856
    }
    
    0 讨论(0)
提交回复
热议问题