6
array = Array.new(10) { Array.new(10 , 0)}

array.each { |x| print x }

打印出一行 10 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

如果我要更改printputs,那么我会在页面下方获得 100 0

如何在没有“[]”和“,”的情况下在单独的行上打印每个数组?

就像是:

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
4

4 回答 4

11

认为:

arr = Array.new(10) { (0..20).to_a.sample(10) }

然后

puts arr.map { |x| x.join(' ') }
1 9 6 15 7 19 18 3 0 12
13 20 18 15 0 3 19 1 14 16
7 16 5 3 12 19 4 9 20 10
6 10 9 1 18 17 7 19 5 15
12 3 8 16 10 5 2 18 20 6
12 9 0 18 2 11 16 8 7 15
8 9 14 19 3 16 6 20 13 17
7 19 16 14 13 6 9 2 3 5
10 17 8 15 11 2 13 14 16 7
14 9 20 17 15 3 4 2 11 19

不是很,呃,有吸引力。对于更令人愉悦的事情,您可以很容易地执行以下操作:

width = arr.flatten.max.to_s.size+2
  #=> 4
puts arr.map { |a| a.map { |i| i.to_s.rjust(width) }.join }
   1   9   6  15   7  19  18   3   0  12
  13  20  18  15   0   3  19   1  14  16
   7  16   5   3  12  19   4   9  20  10
   6  10   9   1  18  17   7  19   5  15
  12   3   8  16  10   5   2  18  20   6
  12   9   0  18   2  11  16   8   7  15
   8   9  14  19   3  16   6  20  13  17
   7  19  16  14  13   6   9   2   3   5
  10  17   8  15  11   2  13  14  16   7
  14   9  20  17  15   3   4   2  11  19

如果屏幕上显示的列太多,可以这样做:

puts arr.map { |a| a.map { |i| i.to_s.rjust(width) }.join.tinyfy }
    1 9 6 15 7 19 18 3 0 12
   13 20 18 15 0 3 19 1 14 16
    7 16 5 3 12 19 4 9 20 10
    6 10 9 1 18 17 7 19 5 15
   12 3 8 16 10 5 2 18 20 6
   12 9 0 18 2 11 16 8 7 15
    8 9 14 19 3 16 6 20 13 17
    7 19 16 14 13 6 9 2 3 5
   10 17 8 15 11 2 13 14 16 7
   14 9 20 17 15 3 4 2 11 19
于 2014-12-05T19:55:51.897 回答
6

尝试加入

array.each { |x|
 puts x.join(" ")
}
# prints:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
于 2014-12-05T13:38:34.210 回答
3

打印列间距的二维数组

接受具有 to_s 方法(字符串整数浮点数和布尔值..)和可选边距宽度整数的对象的二维数组。

更新:它现在适用于不同长度的数组。

def print_table(table, margin_width = 2)
  # the margin_width is the spaces between columns (use at least 1)

  column_widths = []
  table.each do |row|
    row.each.with_index do |cell, column_num|
      column_widths[column_num] = [column_widths[column_num] || 0, cell.to_s.size].max
    end
  end

  puts (table.collect do |row|
    row.collect.with_index do |cell, column_num|
      cell.to_s.ljust(column_widths[column_num] + margin_width)
    end.join
  end)
end

注意: puts 语句后面的括号是必需table.collect的,因此do end块不会作为两个单独的参数传递给 puts 方法。

示例表

my_table = [
  ["1", "Animal", "Dog", "1"],
  [1, "Animal", "Cat", "2"],
  [1, "Animal", "Bird", "3"],
  [2, "Place", "USA", "1"],
  [2.5, "Place", "Other", "2"],
  [3, "Color", "Red"],
  [3, "Color", "Blue", "b"],
  [3, "Some more color", "Orange", "c"],
  [4.7, "Age", "Young", "a"],
  [4, "Age", "Middle", "b"],
  [4, "Age", "Old", "c"],
  [5, "Alive"],
  [],
  [5, "Alive", false, "n"]
]

print_table my_table

印刷:

1    Animal           Dog     1  
1    Animal           Cat     2  
1    Animal           Bird    3  
2    Place            USA     1  
2.5  Place            Other   2  
3    Color            Red     
3    Color            Blue    b  
3    Some more color  Orange  c  
4.7  Age              Young   a  
4    Age              Middle  b  
4    Age              Old     c  
5    Alive            

5    Alive            false   n 

(没有上色。上面的颜色是由 StackOverflow 添加的。)

于 2018-04-22T20:40:03.947 回答
2

您可能想编写自己的方法来做到这一点。就像是:

def array_2D_print array
    array.each do |arr|
        arr.each do |item|
            print "#{item} "
        end
        print "\n"
    end
end

如果您只在代码中使用它一次,您也可以考虑不创建任何方法:

array.each do |arr|
    arr.each do |item|
        print "#{item} "
    end
    print "\n"
end

此解决方案的优点是比其他替代方案更容易修改,以匹配您想要打印的内容。

于 2014-12-05T13:41:56.223 回答