是否有内置方法或gem可以按列打印Ruby数组?

是否有内置方法或gem可以按列打印Ruby数组?,ruby,pretty-print,Ruby,Pretty Print,我有一个很大的Ruby数组,我希望以列形式打印,至少与OS X上Unix“ls”命令的默认输出类似。有没有一个gem或内置的方法可以做到这一点?我知道这是一颗非常棒的宝石。它有一些性感的输出,但似乎没有提供专栏。可能是你的朋友 $ irb irb> a = (0..18).to_a => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] irb> a.each_slice(5) { |row|

我有一个很大的Ruby数组,我希望以列形式打印,至少与OS X上Unix“ls”命令的默认输出类似。有没有一个gem或内置的方法可以做到这一点?我知道这是一颗非常棒的宝石。它有一些性感的输出,但似乎没有提供专栏。

可能是你的朋友

$ irb
irb> a = (0..18).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
irb> a.each_slice(5) { |row| puts row.map{|e| "%5d" % e}.join("  ") }
    0      1      2      3      4
    5      6      7      8      9
   10     11     12     13     14
   15     16     17     18
如果希望在列中对它们进行排序,可以使用slice和

可能是你的朋友

$ irb
irb> a = (0..18).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
irb> a.each_slice(5) { |row| puts row.map{|e| "%5d" % e}.join("  ") }
    0      1      2      3      4
    5      6      7      8      9
   10     11     12     13     14
   15     16     17     18
如果希望在列中对它们进行排序,可以使用slice和

我认为使用hirb更有用:

require 'hirb'

Hirb.enable :output=>{"Array"=>{:class=>Hirb::Helpers::Table}}  #=> true

[[1,2], [2,3]]
+---+---+
| 0 | 1 |
+---+---+
| 1 | 2 |
| 2 | 3 |
+---+---+
2 rows in set

[[5,6,3,4]]
+---+---+---+---+
| 0 | 1 | 2 | 3 |
+---+---+---+---+
| 5 | 6 | 3 | 4 |
+---+---+---+---+
1 row in set
我认为使用hirb更有用:

require 'hirb'

Hirb.enable :output=>{"Array"=>{:class=>Hirb::Helpers::Table}}  #=> true

[[1,2], [2,3]]
+---+---+
| 0 | 1 |
+---+---+
| 1 | 2 |
| 2 | 3 |
+---+---+
2 rows in set

[[5,6,3,4]]
+---+---+---+---+
| 0 | 1 | 2 | 3 |
+---+---+---+---+
| 5 | 6 | 3 | 4 |
+---+---+---+---+
1 row in set

我同意评论者肖恩的观点,但我只是不能控制住我的情绪,而是控制住我的小便,让这个可爱的小家伙感受一下。我在Windows上,所以不知道ls的输出是如何相似的,但我相信这里有足够的选项来给你想要的输出

cm = {'headers' => ['first', 'second', 'third', 'fourth'], 'width' => [5, 5, 16, 30], 'separator' => '|', 'align' => [:left,:right,:left,:right]}
a = [["on", "two", "three", "a loooooooooooooooonger field"],["four","five","looooooooooonger","short one"]]
cm['headers'].each_with_index{|header, index|w = cm['width'][index];print "#{cm['align'][index]==:left ? header.ljust(w)[0..w-1]:header.rjust(w)[0..w-1]}#{cm['separator']}"}
puts ""
a.each do |record|
  record.each_with_index do |field, index|
    w = cm['width'][index]
    print "#{cm['align'][index]==:left ? field.ljust(w)[0..w-1]:field.rjust(w)[0..w-1]}#{cm['separator']}"
  end
  puts ""
end
给予


我同意评论者肖恩的观点,但我只是不能控制住我的情绪,而是控制住我的小便,让这个可爱的小家伙感受一下。我在Windows上,所以不知道ls的输出是如何相似的,但我相信这里有足够的选项来给你想要的输出

cm = {'headers' => ['first', 'second', 'third', 'fourth'], 'width' => [5, 5, 16, 30], 'separator' => '|', 'align' => [:left,:right,:left,:right]}
a = [["on", "two", "three", "a loooooooooooooooonger field"],["four","five","looooooooooonger","short one"]]
cm['headers'].each_with_index{|header, index|w = cm['width'][index];print "#{cm['align'][index]==:left ? header.ljust(w)[0..w-1]:header.rjust(w)[0..w-1]}#{cm['separator']}"}
puts ""
a.each do |record|
  record.each_with_index do |field, index|
    w = cm['width'][index]
    print "#{cm['align'][index]==:left ? field.ljust(w)[0..w-1]:field.rjust(w)[0..w-1]}#{cm['separator']}"
  end
  puts ""
end
给予


除了我的第一个完整的可配置解决方案之外,这里还有一个基于元素的最大字符串长度的较短的解决方案

class Array
  def to_table l = []
    self.each{|r|r.each_with_index{|f,i|l[i] = [l[i]||0, f.length].max}}
    self.each{|r|r.each_with_index{|f,i|print "#{f.ljust l[i]}|"};puts ""}
  end
end

[["on", "two", "three", "a loooooooooooooooonger field"],["four","five","looooooooooonger","short one"]].to_table
给予


除了我的第一个完整的可配置解决方案之外,这里还有一个基于元素的最大字符串长度的较短的解决方案

class Array
  def to_table l = []
    self.each{|r|r.each_with_index{|f,i|l[i] = [l[i]||0, f.length].max}}
    self.each{|r|r.each_with_index{|f,i|print "#{f.ljust l[i]}|"};puts ""}
  end
end

[["on", "two", "three", "a loooooooooooooooonger field"],["four","five","looooooooooonger","short one"]].to_table
给予



手动编写代码应该不会太难。这是5行代码:发布一些示例数据和一些示例输出如何?我的水晶球今天离线了。@Sergio:我在这里问这个问题的原因不是因为我不知道如何手动编码,而是因为我在标准库中寻找一些东西,或者我需要一块宝石,以便缩短我自己的代码。@CodeGnome:我不确定你在示例代码中寻找什么。这是一个关于存在什么的简单问题,我不是在寻找多行代码解决方案。我意识到我应该改变标题来反映这一点。很抱歉你的水晶球,我也有这种情况。手动编写代码应该不会太难。这是5行代码:发布一些示例数据和一些示例输出如何?我的水晶球今天离线了。@Sergio:我在这里问这个问题的原因不是因为我不知道如何手动编码,而是因为我在标准库中寻找一些东西,或者我需要一块宝石,以便缩短我自己的代码。@CodeGnome:我不确定你在示例代码中寻找什么。这是一个关于存在什么的简单问题,我不是在寻找多行代码解决方案。我意识到我应该改变标题来反映这一点。对不起,你的水晶球,我也一样。上帝啊,那是一个可怕的小可爱。我很感激它完成了任务,但有更简单的解决方案。我选择成熟的和可重复使用的,虽然我认为它可以做得更短,但这超出了我目前对鲁比神的了解,这是一个可怕的可爱。我很感激它完成了任务,但有更简单的解决方案。我选择成熟的和可重用的,虽然我认为它可以做得更短,但这超出了我目前对RubyVery slick的了解。我会用这个。我仍然在想,是否有一个标准的库或通用的gem函数可以处理这个问题,也许可以自动调整列间距。@Sean:确实非常流畅,我喜欢那些真正教会你一些东西的答案,谢谢dbenhur,但您是否意识到,当您包含一个gem时,您可能会包含数千行代码?那么为什么还要费心编写几行代码来完成这项工作呢?非常流畅。我会用这个。我仍然在想,是否有一个标准的库或通用的gem函数可以处理这个问题,也许可以自动调整列间距。@Sean:确实非常流畅,我喜欢那些真正教会你一些东西的答案,谢谢dbenhur,但您是否意识到,当您包含一个gem时,您可能会包含数千行代码?那么,为什么还要费心编写几行代码来完成这项工作呢?太棒了。这正是我想要的。在您的示例中,我看到您必须为它提供一个数组数组,因此如果您只有一个1D数组,则需要首先使用Enumerableeach_切片。这是一件小事,但我仍然在想:hirb能自动调整1D阵列吗?太棒了。这正是我想要的。在您的示例中,我看到您必须为它提供一个数组数组,因此如果您只有一个1D数组,则需要首先使用Enumerableeach_切片。这是一件小事,但我仍然在想:hirb可以自动扩展1D阵列吗?这看起来是一个不错的扩展,比另一个更干净。这看起来是一个不错的扩展,比另一个更干净。