Skip to content

Null (Blackhole) Output

Jeff Felchner edited this page Sep 14, 2017 · 1 revision

There are rare instances where you may not want any output from the progressbar to appear on the screen or in a file.

In a case like this, the user doesn't ever want to see the progressbar and instead only ever wants to access it via #to_s. A use case might be a CLI program which, when executed, prints the current status of multiple bars (maybe with extra information before or after it) and then exits. Since by default progressbar prints itself to stdout on every update, there would be undesirable output for the above case.

ruby-progressbar has an official solution for this use case: ProgressBar::Outputs::Null When used, it will effectively ignore any calls to output the bar.

You can use it like this:

require 'ruby-progressbar/outputs/null'

progressbar = ProgressBar.create(output: ProgressBar::Outputs::Null)

if you then attempt to do anything with the bar:

progressbar.increment
progressbar.reset
progressbar.clear
progressbar.progress += 20
progressbar.finish

you would not see any output for anything that happened above.