pretty print to a file in ruby

后端 未结 7 796
甜味超标
甜味超标 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
    }
    

提交回复
热议问题