Skip to content

Output as Hash

Jeff Felchner edited this page Sep 15, 2017 · 6 revisions

In various use cases, it's handy to have access to the data that the progressbar has, but without actually printing the bar. Maybe you want to send the current percentage completed to a file to build some benchmarking metrics or maybe after every completed run you want to clear the bar but print the elapsed time to the screen in a footer of some sort.

In order to do this, simply call to_h on the progressbar instance and you'll get access to a plethora of information such as:

  • Output Stream IO Object
  • Length of the Bar
  • Title
  • Progress Mark
  • Remainder Mark
  • Current Progress
  • Total
  • Percentage Completed
  • Elapsed Time In Seconds
  • Estimated Time Remaining In Seconds
  • Rate Of Change
  • Rate Of Change (Using the User's Custom Rate Scale)
  • Unknown Progress Animation Steps
  • Throttle Rate
  • Whether the bar is started (including the time it was started)
  • Whether the bar is stopped
  • Whether the bar is finished

Example

Given this input:

progressbar = ProgressBar::Base.new(:output         => output,
                                    :total          => 33,
                                    :title          => 'My Title',
                                    :progress_mark  => 'x',
                                    :remainder_mark => '-',
                                    :length         => 92,
                                    :rate_scale     => lambda { |rate| rate * 200 },
                                    :throttle_rate  => 12.3)

# Make some progress updates over a period of time

progressbar.to_h

this would output something like:

{
  'output_stream'                       => #<StringIO:0x007fdf2d056928>,
  'length'                              => 92,
  'title'                               => 'My Title',
  'progress_mark'                       => 'x',
  'remainder_mark'                      => '-',
  'progress'                            => 22,
  'total'                               => 33,
  'percentage'                          => 66.66,
  'elapsed_time_in_seconds'             => 599.99994701264,
  'estimated_time_remaining_in_seconds' => 400,
  'base_rate_of_change'                 => 0.03672787979966611,
  'scaled_rate_of_change'               => 7.345575959933222,
  'unknown_progress_animation_steps'    => ['=---', '-=--', '--=-', '---='],
  'throttle_rate'                       => 12.3,
  'started?'                            => 2012-07-26 12:50:00 -0500,
  'stopped?'                            => false,
  'finished?'                           => false
}

Go forth and conquer!