如何使用 Ruby 中的循环创建具有不同列数的数组?
How to create arrays with different number of columns using a loop in Ruby?
我需要创建具有不同列数的数组,具体取决于某些变量值是 "Show" 还是 "Hide"。
activity_types
有很多activities
activity_type
属于一个用户
activities
会有多个columns: date, duration, cost, subject...
activity_types
每个 activity 列都有对应的切换列,
例如:subject_toggle
、`duration_toggle
这些切换列的值只能是 "Show" 或 "Hide"
所以我想在 prawnpdf 中为所有不同的 activity_types 创建一系列 table。每个 table 应该显示不同的列集,具体取决于该列的 activity_type
切换变量是 "show" 还是 "hide"
我开始研究这个,但很快就不知道它是如何有条件地在数组中创建列的。我可以将其留空(有一个 if 语句,如果它的 "Hide" 会输出空白)但它不会删除该列。
下面是我为 prawnpdf.
创建 table 的代码
ActivityType.all.each do |type|
define_method("method_#{type.id}") do
move_down 20
font "Nunito"
text type.title, :align => :left, size: 13, style: :bold
move_down 5
table eval("rows_#{type.id}"), :position => :center, :width => 540, :column_widths => {0 => 50, 8 => 60},
:cell_style => {:font => "Nunito", :size => 10} do
row(0).font_style = :bold
columns(0..8).align = :center
self.row_colors = ["F0F0F0", "FFFFFF"]
self.header = true
end
end end
ActivityType.all.each do |type|
define_method("rows_#{type.id}") do
[["Date", "Subject", "Details", "Time (min)","Contact","Detail","Outcome","Agent",""]] +
type.activities.order(date: :asc).map do |activity|
[activity.date.strftime("%b %d"), activity.subject, activity.contact, activity.detail,
activity.outcome, activity.agent, activity.customer, activity.cost, activity.duration.to_i ]
end
end end
您可以使用 Array#compact
从数组中删除空元素:
https://ruby-doc.org/core-2.1.2/Array.html#method-i-compact
compact → new_ary
Returns a copy of self with all nil elements removed.
[ "a", nil, "b", nil, "c", nil ].compact
#=> [ "a", "b", "c" ]
但是,它只会删除 nil
个值。如果您的值也可以是空字符串,您可以使用 reject
.
在您的特定情况下,它将是:
[activity.date.strftime("%b %d"), activity.subject, activity.contact, activity.detail,activity.outcome, activity.agent, activity.customer, activity.cost, activity.duration.to_i ].reject { |el| el.nil? || el.empty? }
我需要创建具有不同列数的数组,具体取决于某些变量值是 "Show" 还是 "Hide"。
activity_types
有很多activities
activity_type
属于一个用户activities
会有多个columns: date, duration, cost, subject...
activity_types
每个 activity 列都有对应的切换列, 例如:subject_toggle
、`duration_toggle
这些切换列的值只能是 "Show" 或 "Hide"
所以我想在 prawnpdf 中为所有不同的 activity_types 创建一系列 table。每个 table 应该显示不同的列集,具体取决于该列的 activity_type
切换变量是 "show" 还是 "hide"
我开始研究这个,但很快就不知道它是如何有条件地在数组中创建列的。我可以将其留空(有一个 if 语句,如果它的 "Hide" 会输出空白)但它不会删除该列。
下面是我为 prawnpdf.
创建 table 的代码ActivityType.all.each do |type|
define_method("method_#{type.id}") do
move_down 20
font "Nunito"
text type.title, :align => :left, size: 13, style: :bold
move_down 5
table eval("rows_#{type.id}"), :position => :center, :width => 540, :column_widths => {0 => 50, 8 => 60},
:cell_style => {:font => "Nunito", :size => 10} do
row(0).font_style = :bold
columns(0..8).align = :center
self.row_colors = ["F0F0F0", "FFFFFF"]
self.header = true
end
end end
ActivityType.all.each do |type|
define_method("rows_#{type.id}") do
[["Date", "Subject", "Details", "Time (min)","Contact","Detail","Outcome","Agent",""]] +
type.activities.order(date: :asc).map do |activity|
[activity.date.strftime("%b %d"), activity.subject, activity.contact, activity.detail,
activity.outcome, activity.agent, activity.customer, activity.cost, activity.duration.to_i ]
end
end end
您可以使用 Array#compact
从数组中删除空元素:
https://ruby-doc.org/core-2.1.2/Array.html#method-i-compact
compact → new_ary
Returns a copy of self with all nil elements removed.
[ "a", nil, "b", nil, "c", nil ].compact
#=> [ "a", "b", "c" ]
但是,它只会删除 nil
个值。如果您的值也可以是空字符串,您可以使用 reject
.
在您的特定情况下,它将是:
[activity.date.strftime("%b %d"), activity.subject, activity.contact, activity.detail,activity.outcome, activity.agent, activity.customer, activity.cost, activity.duration.to_i ].reject { |el| el.nil? || el.empty? }