-4

我在一个数组中有 Ruby 数组,如下所示:

更多数据信息在这里https://gist.github.com/theCrab/6064789

列标题为stylecolorsizedescqtymin_qtyreq_qtyroot_skuoffset_size

[
["UD/BOYS-TR/SENIOR/STANDARD", "*BLACK/2SHORT LEG", "28\" W", "29\"LEG STD 男式长裤", -10, 4, 14, "00001955", 1 ],
["UD/BOYS-TR/SENIOR/STANDARD", "*BLACK/2SHORT LEG", "30\" W", "29\"LEG STD 男式长裤", -14, 6, 20, "00001955", 2 ],
["UD/BOYS-TR/SENIOR/STANDARD", "*BLACK/2SHORT LEG", "32\" W", "29\"LEG STD 男式长裤", -29, 6, 35, "00001955", 3 ],
[“UD/BOYS-TR/SENIOR/STANDARD”、“*BLACK/3REG LEG”、“28\”W”、“31\”LEG STD 男式长裤”、0、3、3、“00001976”、1] ,
["UD/BOYS-TR/SENIOR/STANDARD", "*BLACK/3REG LEG", "30\" W", "31\"LEG STD 男式长裤", -12, 5, 17, "00001976", 2 ],
["UD/BOYS-TR/SENIOR/STANDARD", "*BLACK/3REG LEG", "32\" W", "31\"LEG STD 男式长裤", -6, 6, 12, "00001976", 3 ]
]

我想打印一个格式良好的表格,如下所示:

style | colour | size | desc | qty | min_qty | req_qty | root_sku
-----------------------------------------------------------------
standd| red    | 28 W | N/a  | 4   | 5       | 1       | 00001955
      |        | 30 W | N/a  | 9   | 5       | 4       | 00001955
      |        | 36 W | N/a  | 10  | 5       | 5       | 00001955
      | blue   | 28 W | N/a  | 4   | 5       | 1       | 00001955
      |        | 30 W | N/a  | 9   | 5       | 4       | 00001955
      |        | 36 W | N/a  | 10  | 5       | 5       | 00001955
fitted| red/blu| 28 W | N/a  | 4   | 5       | 1       | 00001954
      |        | 30 W | N/a  | 9   | 5       | 4       | 00001954
      |        | 36 W | N/a  | 10  | 5       | 5       | 00001954
      | blue   | 28 W | N/a  | 4   | 5       | 1       | 00001954
      |        | 30 W | N/a  | 9   | 5       | 4       | 00001954
      |        | 36 W | N/a  | 10  | 5       | 5       | 00001954

我怎样才能最好地穿过阵列并完成这一壮举?

4

1 回答 1

2

Ruby 有一个字符串格式化功能,我认为它源自旧的 C 风格sprintf函数。http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-sprintfhttp://ruby-doc.org/core-2.0/String.html#method-i-25

在 String 类中,您使用该%方法并传入一个数组。

outer_array.each do |inner_array|
  puts ('%6s|%8s|%5s|%6s|%4d|%8d|%6d|%10d' % inner_array)
end

(PS我只是猜到了格式,所以你需要根据需要进行改进)

于 2013-07-24T14:29:47.063 回答