散列的最后一个元素
last element of the hash
我正在发送几个用于打印 pdf 票据的参数
Parameters: {"utf8"=>"✓", "list"=>{"client_0801541"=>"0801541", "client_0801554"=>"0801554", "client_0801554"=>"0801554"}, "subaction"=>"print selected clients"}
我需要在不同的页面上打印每个客户,为此我在显示客户代码的末尾使用了 start_new_page,但在最后一个客户留下空白页后以这种方式。我怎样才能让它为每个客户端验证它是否是数组的最后一个?我尝试了以下方法:
def client
@client.each do |(c,client_id)|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless client_id == @client.map{|e|[e.c, e.client_id]}.client_id.last
end
end
你可以试试:
@clients.each do |(c, client_id)|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless client_id == @clients.values.last
end
您也可以试试:
@clients.each_with_index do |(c, client_id), i|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless i == @clients.length - 1
end
正如 Tadman 所建议的,您还可以通过以下方式加快速度:
def client
@clients_length = @clients.length - 1
@clients.each_with_index do |(c, client_id), i|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless i == @clients_length
end
end
您的原代码:
def client
@client.each do |(c,client_id)|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless client_id == @client.map{|e|[e.c, e.client_id]}.client_id.last
end
end
至少看起来有点受伤,因为:
- 您在每次迭代中都使用
map
,这看起来很浪费。
- 在
@client.map{|e|[e.c, e.client_id]}
中,.c
和 .client_id
不是 Array
上的方法。
.client_id
不是 Array
上的方法。
- 可能是其他东西。
顺便说一句,您 list
中的最后两个 keys
和 values
是相同的。我不知道这是不是一个错误。但是,如果那是你想要的,那么你可能会有其他问题。
顺便说一句,你的问题的标题是 'last element of the array',但你使用的是 hash
,而不是 array
。
我正在发送几个用于打印 pdf 票据的参数
Parameters: {"utf8"=>"✓", "list"=>{"client_0801541"=>"0801541", "client_0801554"=>"0801554", "client_0801554"=>"0801554"}, "subaction"=>"print selected clients"}
我需要在不同的页面上打印每个客户,为此我在显示客户代码的末尾使用了 start_new_page,但在最后一个客户留下空白页后以这种方式。我怎样才能让它为每个客户端验证它是否是数组的最后一个?我尝试了以下方法:
def client
@client.each do |(c,client_id)|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless client_id == @client.map{|e|[e.c, e.client_id]}.client_id.last
end
end
你可以试试:
@clients.each do |(c, client_id)|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless client_id == @clients.values.last
end
您也可以试试:
@clients.each_with_index do |(c, client_id), i|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless i == @clients.length - 1
end
正如 Tadman 所建议的,您还可以通过以下方式加快速度:
def client
@clients_length = @clients.length - 1
@clients.each_with_index do |(c, client_id), i|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless i == @clients_length
end
end
您的原代码:
def client
@client.each do |(c,client_id)|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless client_id == @client.map{|e|[e.c, e.client_id]}.client_id.last
end
end
至少看起来有点受伤,因为:
- 您在每次迭代中都使用
map
,这看起来很浪费。 - 在
@client.map{|e|[e.c, e.client_id]}
中,.c
和.client_id
不是Array
上的方法。 .client_id
不是Array
上的方法。- 可能是其他东西。
顺便说一句,您 list
中的最后两个 keys
和 values
是相同的。我不知道这是不是一个错误。但是,如果那是你想要的,那么你可能会有其他问题。
顺便说一句,你的问题的标题是 'last element of the array',但你使用的是 hash
,而不是 array
。