使用表格打印gem使用Ruby格式化

问题描述:

我的Json文件的输出是散列。我遍历每一行并使用tp gem格式化它。我的问题是如何格式化宽度,以便我的所有数据在终端中整齐地显示。使用表格打印gem使用Ruby格式化

我认为它不是格式整齐,因为散列内的数组。

File.open("output.json", 'a+') do |file| 
     1  data = { time_format: time_format, purchase: @purchase, quantity: @quantity, retail_price: @retail_price, purchasing_price: @purchasing_price } 
     0  file.puts data.to_json                                   
     1  end 
transaction = File.open('output.json', 'a+').each do |line| 
      @transaction = JSON.parse(line) 
      if @transaction["time_format"].include?(review_date) 

      end  
     end  
     tp report 



TIME_FORMAT  | PURCHASE | QUANTITY | RETAIL_PRICE | PURCHASING_PRICE 
-----------------|----------|----------|--------------|----------------- 
11/23/2013 08:07 | ["lig... | [1]  | [5.0]  | [3.0]   
11/23/2013 08:08 | ["lig... | [1]  | [5.0]  | [3.0]   
11/23/2013 08:08 | ["lig... | [1, 1... | [5.0, 7.5... | [3.0, 4.0, 5.... 
11/23/2013 08:12 | ["lig... | [1]  | [5.0]  | [3.0]   
11/23/2013 08:13 | ["lig... | [1]  | [5.0]  | [3.0]   
11/23/2013 08:18 | ["lig... | [1]  | [5.0]  | [3.0]   

编辑:宝石被称为Table Print

+0

请提供一个链接到宝石你使用,我无法在rubygems.org上找到'tp'gem的表格 –

有几个方法可以改变列宽。

要改变所有列的:

tp.set :max_width, 60

要更改单个列,打印像往常一样,但给一些列选项:

tp report, :include => [:purchase => {:width => 60}]