Skip to content

Instantly share code, notes, and snippets.

@walm
Forked from bmarini/progress_bar.rb
Created January 4, 2014 12:00
Show Gist options
  • Save walm/8254736 to your computer and use it in GitHub Desktop.
Save walm/8254736 to your computer and use it in GitHub Desktop.
class ProgressBar
def initialize(units=60)
@units = units.to_f
end
def print(completed, total)
norm = 1.0 / (total / @units)
progress = (completed * norm).ceil
pending = @units - progress
Kernel.print "[#{'=' * progress }#{' ' * ( pending )}] #{percentage(completed, total)}%\r"
end
def percentage(completed, total)
( ( completed / total.to_f ) * 100 ).round
end
end
# Usage:
#
# bar = ProgressBar.new
# bar.print(50, 100)
#
# => [============================== ] 50%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment