Skip to content

Rate Scaling

bjmfactory edited this page Nov 18, 2014 · 1 revision

By default the rate shown on the progress bar is shown as the number of items processed per second. Often times, you won't want to display the literal number of items, but rather scale it in some way. For example, if you're displaying the progress of a file being transferred, and you are representing the bar as the number of bytes in the file, by default the %r formatter will show the number of bytes per second.

If instead of the bytes per second you wanted to show the user the number of kilobytes per second, you can do that like so:

ProgressBar.create(:format     => '%a %B %p%% %r KB/sec',
                   :rate_scale => lambda { |rate| rate / 1024 })

Now, when the bar is displayed it will look something like so:

Time: --:--:-- ===============                                40% - 102 KB/sec