ruby 编写控制台进度条

ruby 中,$stdout.flush 让控制台当前行内容可以重写,以此我们可以做出进度条的效果。

def set_progress(index, char = '*')
    print (char * (index / 2.5).floor).ljust(40, " "), " #{index}%\r"
end

100.times do |i|
    set_progress(i + 1)
    $stdout.flush
    sleep 0.05
end
puts

猜你喜欢

转载自www.cnblogs.com/linying1991/p/12021864.html