Skip to content

Instantly share code, notes, and snippets.

@adamgamble
Created May 28, 2010 13:26
Show Gist options
  • Save adamgamble/417140 to your computer and use it in GitHub Desktop.
Save adamgamble/417140 to your computer and use it in GitHub Desktop.
module Enumerable
def each_with_progress(&block)
out = STDERR
self.each_with_index do |thing,i|
out.print sprintf("%s", makeProgress(i,self.count)) + "\r"
block.call thing
end
nil
end
protected
def makeProgress(i, t)
percent = (Float(i) / Float(t)) * 100
percent = (( percent / 1).round * 1).to_i
number_of_bars = percent / 5
progress = ""
for g in 0..number_of_bars do
progress = progress + "="
end
for s in 0..(19 - number_of_bars) do
progress = progress + " "
end
if percent < 10 && percent != 100
progress = progress + " "
end
return percent.to_s + "% |" + progress + "|"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment