【问题标题】:Console table format in rubyruby 中的控制台表格式
【发布时间】:2016-03-22 13:53:44
【问题描述】:

有没有办法将数组的数组输出为 ruby​​ 中的表?我正在从控制台寻找有用的东西,比如 powershell 的 format-table 命令。

要传递的数组可能是这样的:

a = [[1.23, 5, :bagels], [3.14, 7, :gravel], [8.33, 11, :saturn]]

输出可能是这样的:

-----------------------
| 1.23 |  5 | bagels  |
| 3.14 |  7 | gravel  |
| 8.33 | 11 | saturn  |
-----------------------

【问题讨论】:

标签: ruby console


【解决方案1】:

我接受了 sagarpandya82 的回答,但这是我更好的实现:

class Array
  def to_table
    column_sizes = self.reduce([]) do |lengths, row|
      row.each_with_index.map{|iterand, index| [lengths[index] || 0, iterand.to_s.length].max}
    end
    puts head = '-' * (column_sizes.inject(&:+) + (3 * column_sizes.count) + 1)
    self.each do |row|
      row = row.fill(nil, row.size..(column_sizes.size - 1))
      row = row.each_with_index.map{|v, i| v = v.to_s + ' ' * (column_sizes[i] - v.to_s.length)}
      puts '| ' + row.join(' | ') + ' |'
    end
    puts head
  end
end

【讨论】:

  • +1。很好的解决方案。我的答案真的不如你,所以你真的应该接受你自己的答案!
  • @sagarpandya82 在我的文化中,接受你自己的答案被认为是不礼貌的。
【解决方案2】:

你可以试试这样的:

a = [[1.23, 5, :bagels], [3.14, 7, :gravel], [8.33, 11, :saturn]]

bar = '-'*(a[0][0].to_s.length + 4 + a[0][1].to_s.length + 3 + a[0][2].to_s.length + 4) 

puts bar
a.each do |i|
  i.each.with_index do |j,k|
    if k == 1 && j < 10
      print "|  #{j} "
    else
      print "| #{j} "
    end
  end
  print '|'
  puts
end
puts bar

返回:

----------------------
| 1.23 |  5 | bagels |
| 3.14 |  7 | gravel |
| 8.33 | 11 | saturn |
----------------------

bar 只是对顶部和底部短划线长度的估计。通常,此代码检查每个子数组并在适当的位置用| 打印出它的元素。唯一棘手的一点是每个子数组的第二个元素。在这里,我们检查它是两位数还是一位数。每种的打印格式都不同。

请记住,此代码专门适用于您的示例,并且做了很多假设。话虽如此,它可以很容易地根据口味进行修改。

【讨论】:

    【解决方案3】:

    我对@dart-egregious sn-p 做了一个小改进

    class Array
      def to_table(header: true)
        column_sizes = self.reduce([]) do |lengths, row|
          row.each_with_index.map{|iterand, index| [lengths[index] || 0, iterand.to_s.length].max}
        end
        head = '+' + column_sizes.map{|column_size| '-' * (column_size + 2) }.join('+') + '+'
        puts head
    
        to_print = self.clone
        if (header == true)
          header = to_print.shift
          print_table_data_row(column_sizes, header)
          puts head
        end
        to_print.each{ |row| print_table_data_row(column_sizes, row) }
        puts head
      end
    
      private
      def print_table_data_row(column_sizes, row)
        row = row.fill(nil, row.size..(column_sizes.size - 1))
        row = row.each_with_index.map{|v, i| v = v.to_s + ' ' * (column_sizes[i] - v.to_s.length)}
        puts '| ' + row.join(' | ') + ' |'
      end
    end
    

    使用:

    data = [
      ['column 1', 'column 2', 'ciolumn 3'],
      ['row 1 col 2', 'row 1 col 2', 'row 1 col 3'],
      ['row 1 col 2', 'row 1 col 2', 'row 1 col 3']
    ]
    
    data.to_table
    +-------------+-------------+-------------+
    | column 1    | column 2    | ciolumn 3   |
    +-------------+-------------+-------------+
    | row 1 col 2 | row 1 col 2 | row 1 col 3 |
    | row 1 col 2 | row 1 col 2 | row 1 col 3 |
    +-------------+-------------+-------------+
    
    
    data.to_table(header: false)
    +-------------+-------------+-------------+
    | column 1    | column 2    | ciolumn 3   |
    | row 1 col 2 | row 1 col 2 | row 1 col 3 |
    | row 1 col 2 | row 1 col 2 | row 1 col 3 |
    +-------------+-------------+-------------+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多