Skip to content

Instantly share code, notes, and snippets.

@germsvel
Created April 17, 2018 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save germsvel/f410704761501e4b86b259121cce3bca to your computer and use it in GitHub Desktop.
Save germsvel/f410704761501e4b86b259121cce3bca to your computer and use it in GitHub Desktop.
class ProgressBar
# Prints progress like this:
# Progress: (15%) [==== ]
def initialize(total)
@total = total
@counter = 0
@bar_width = 50
end
def inc
@counter += 1
print_progress_bar
end
def end
@counter = @total
print_progress_bar
end
private
def print_progress_bar
printf(
"\rProgress: (%-.2f%%) [%-#{@bar_width}s]",
percent_complete,
"=" * bar_increments_complete
)
end
def bar_increments_complete
fraction_complete * @bar_width
end
def percent_complete
fraction_complete * 100
end
def fraction_complete
@counter.to_f / @total.to_f
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment