Class: ProgressBar
- Inherits:
-
Object
- Object
- ProgressBar
- Defined in:
- lib/progressbar.rb
Overview
Copyright 2013 whiteleaf. All rights reserved.
Instance Method Summary collapse
- #calc_ratio(num) ⇒ Object
- #clear ⇒ Object
-
#initialize(max, interval = 1, width = 50, char = "*") ⇒ ProgressBar
constructor
A new instance of ProgressBar.
- #output(num) ⇒ Object
Constructor Details
#initialize(max, interval = 1, width = 50, char = "*") ⇒ ProgressBar
Returns a new instance of ProgressBar.
7 8 9 10 11 12 13 |
# File 'lib/progressbar.rb', line 7 def initialize(max, interval = 1, width = 50, char = "*") @max = max == 0 ? 1.0 : max.to_f @interval = interval @width = width @char = char @counter = 0 end |
Instance Method Details
#calc_ratio(num) ⇒ Object
30 31 32 |
# File 'lib/progressbar.rb', line 30 def calc_ratio(num) num / @max end |
#clear ⇒ Object
25 26 27 28 |
# File 'lib/progressbar.rb', line 25 def clear return if $debug STDOUT.print " " * 79 + "\r" end |
#output(num) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/progressbar.rb', line 15 def output(num) return if $debug @counter += 1 return unless @counter % @interval == 0 ratio = calc_ratio(num) now = (@width * ratio).round rest = @width - now STDOUT.print "[" + @char * now + ' ' * rest + "] #{(ratio * 100).round}%\r" end |