gpt4 book ai didi

ruby - 如何根据已处理项目的数量而不是 "costs"和 "ruby-progressbar"的数量来显示进度?

转载 作者:太空宇宙 更新时间:2023-11-03 16:13:17 27 4
gpt4 key购买 nike

让我们用随机数初始化一个数组。现在让我们创建一个 ProgressBar以所有数字的总和作为其大小。我们可以轻松地遍历所有数字并随着每个数字递增进度条,这为我们提供了非常整洁和精确的进度:

require 'ruby-progressbar'

items = Array.new(100) { rand 1..10 }

progress_bar = ProgressBar.create total: items.sum, format: '%a %e %P% Processed: %c from %C items'

items.each do |item|
sleep item / 100.0
progress_bar.progress += item
end

但是,用户有兴趣查看在进行过程中处理了多少项目,而不是项目的加权成本。现在我们显示例如Processed: 270 from 516 items这是已处理项目的总和与总和的比值。相反,我想显示 Processed: 53 from 100 items , 但后台的总和和增量保持原样,否则进度条将不准确。

ruby-progressbar报价the following flags for formatting .

我没有看到任何用于放置占位符或派生值的选项。

最佳答案

您可以 customize format string in realtime while processing并在格式字符串中自行更新值。下面是修改后的脚本,向您展示我的意思:

require 'ruby-progressbar'

items = Array.new(100) { rand 1..10 }
current = 0
count = items.count
progress_bar = ProgressBar.create total: items.sum, format: "%a %e %P% Processed: #{current} from #{count} items"

items.each do |item|
sleep item / 100.0
current += 1
progress_bar.format = "%a %e %P% Processed: #{current} from #{count} items"
progress_bar.progress += item
end

示例输出:时间:00:00:01 ETA:00:00:04 29.48% 已处理:100 个项目中的 27 个

关于ruby - 如何根据已处理项目的数量而不是 "costs"和 "ruby-progressbar"的数量来显示进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55059820/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com