简体   繁体   English

行的备用格式,每组中有多行

[英]Alternate formatting of rows, with multiple rows in each set

I'm programming in Ruby. 我正在用Ruby编程。

Given that I have the following 2D array: 鉴于我有以下2D数组:

list = [
  ["T1", "Cat 1"],
  ["T2", "Cat 2"],
  ["T3", "Cat 3"],
  ["T4", "Cat 4"],
  ["T5", "Cat 1"],
  ["T6", "Cat 2"],
  ["T7", "Cat 3"],
  ["T8", "Cat 4"],
  ["T9", "Cat 1"],
  ["T10", "Cat 2"],
  ["T11", "Cat 3"],
  ["T12", "Cat 4"],
]

I also have a list of categories: 我也有一个类别列表:

cat = ["Cat 1", "Cat 2", "Cat 3", "Cat 4"]
cat_count = cat.count

I also have the method bold , which formats text to be bold, for simplicity here I am going to use the method below: 我还有方法bold ,它将文本设置为粗体,为简单起见,在这里我将使用以下方法:

def bold(text)
  'b_' << text
end

I would like to format each set of categories, in an alternating fashion. 我想以交替方式设置每组类别的格式。 So the first set would be bold, and then the second set not, the third set bold, the fourth set not etc. 因此,第一组将是粗体,然后第二组将不是粗体,第三组将是粗体,第四组将不如此。

In this case I would get out the following formatted output: T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 在这种情况下,我将得到以下格式化的输出: T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12

I expect the following ruby output: 我期望以下红宝石输出:

["b_T1", "Cat 1"]
["b_T2", "Cat 2"]
["b_T3", "Cat 3"]
["b_T4", "Cat 4"]
["T5", "Cat 1"]
["T6", "Cat 2"]
["T7", "Cat 3"]
["T8", "Cat 4"]
["b_T9", "Cat 1"]
["b_T10", "Cat 2"]
["b_T11", "Cat 3"]
["b_T12", "Cat 4"]

I have the following code at the moment, which does not look very Rubyist at all. 我现在有以下代码,看起来根本不是Rubyist。 Please help me improve this code. 请帮助我改善此代码。

cat_increment = 0
switch = 1

list.map do |l|
  cat_increment+=1
  entry = switch ? [bold(l.first), l.second] : l
  if cat_increment == cat_count
    switch = switch ? nil : 1
    cat_increment = 0
  end 
  entry
end

You could use with_index , array decomposition and some math: 您可以使用with_index数组分解和一些数学运算:

list.map.with_index do |(title, category), index|
  if index % (2 * cat_count) < cat_count
    [bold(title), category]
  else
    [title, category]
  end
end
enum = [*["b_"]*cat.size, *[""]*cat.size].cycle
  #=> #<Enumerator: ["b_", "b_", "b_", "b_", "", "", "", ""]:cycle>
list.map { |t,c| [enum.next+t, c] }
  #=> [["b_T1", "Cat 1"], ["b_T2", "Cat 2"], ["b_T3", "Cat 3"], ["b_T4", "Cat 4"],
  #    ["T5", "Cat 1"], ["T6", "Cat 2"], ["T7", "Cat 3"], ["T8", "Cat 4"],
  #    ["b_T9", "Cat 1"], ["b_T10", "Cat 2"], ["b_T11", "Cat 3"], ["b_T12", "Cat 4"]] 

Array#cycle is used to convert the array ["b_", "b_", "b_", "b_", "", "", "", ""] to an endlessly repeating enumerator. Array#cycle用于将数组["b_", "b_", "b_", "b_", "", "", "", ""]转换为不断重复的枚举器。

It is then simply a matter of mapping each element of list to a new value in which the "T" string is prepended with the next element of enum ( "b_" or "" ). 然后,只需将list每个元素映射到新值即可,在该新值中,“ T”字符串前面带有enum的下一个元素( "b_""" )。

I have chosen to use this enumerator to add "T" prefixes that alternate with each group of cat.size elements, but the more conventional way is to use indices: 我选择使用此枚举器添加与每组cat.size元素交替的“ T”前缀,但更传统的方式是使用索引:

list.map.with_index { |(t,c),i| [(i/4).even? ? "b_"+t : "t", c] } 
  #=> [["b_T1", "Cat 1"], ["b_T2", "Cat 2"], ["b_T3", "Cat 3"], ["b_T4", "Cat 4"],
  #    ["T5", "Cat 1"], ["T6", "Cat 2"], ["T7", "Cat 3"], ["T8", "Cat 4"],
  #    ["b_T9", "Cat 1"], ["b_T10", "Cat 2"], ["b_T11", "Cat 3"], ["b_T12", "Cat 4"]] 

With Ruby 2.3.1 you will be able to write: 使用Ruby 2.3.1,您将能够编写:

list.each_with_index { |(t,_),i| print "#{ (i/4).even? ? t.boldify : t } " }

T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM