百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

tqdm-ruby

> 编程语言
开源

在几秒钟内为您的循环添加进度条(基于 Python 的 tqdm )。

270 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

在几秒钟内为您的循环添加进度条(基于 Python 的 tqdm )。

tqdm-ruby

tqdm-ruby allows you to add a progress indicator to your loops with minimal effort.

It is a port of the excellent tqdm library for python. tqdm (read taqadum, تقدّم) means "progress" in Arabic.

Calling #tqdm (or #with_progress) on any Enumerable returns an enhanced clone that animates a meter during iteration.

ruby
require 'tqdm'
(0...1000).tqdm.each { |x| sleep 0.01 }

The default output is sent to $stderr and looks like this:

|####------| 492/1000  49% [elapsed: 00:05 left: 00:05, 88.81 iters/sec]

It works equally well from within irb, pry, and iRuby notebooks as seen here:

Why not progressbar, ruby-progressbar, powerbar, or any of the other gems? These typically have a bucketload of formatting options and you have to manually send updates to the progressbar object to use them. tqdm pleasantly encourages the laziest usage scenario, in that you "set it and forget it".

Install

Install it globally from Rubygems:

$ gem install tqdm    # (might need sudo for a system ruby)

or add this line to your application's Gemfile:

gem 'tqdm'

And then execute:

$ bundle

Usage

All Enumerable objects gain access to the #with_progress method (aliased as #tqdm), which returns an enhanced object wherein any iteration (by calling #each or any of its relatives: #each_with_index, #map, #select, etc.) produces an animated progress bar on $stderr.

ruby
require 'tqdm'
num = 1629241972611353
(2..Math.sqrt(num)).with_progress.reject { |x| num % x > 0 }.map { |x| [x, num/x] }
# ... Animates a progress bar while calculating...
# => [[32599913, 49976881]]

Options can be provided as a hash, e.g., .with_progress(desc: "copying", leave: true). The following options are available:

  • desc: Short string, describing the progress, added to the beginning of the line
  • total: Expected number of iterations, if not given, self.size || self.count is used
  • file: A file-like object to output the progress message to, by default, $stderr
  • leave: A boolean (default false). Should the progress bar should stay on screen after it's done?
  • min_interval: Default is 0.5. If less than min_interval seconds or min_iters iterations have passed since the last progress meter update, it is not re-printed (decreasing IO thrashing).
  • min_iters: Default is 1. See previous.

Sequel is an amazing database library for Ruby. tqdm can enhance its Dataset objects to show progress while iterating (same options as above):

ruby
require 'tqdm/sequel'   # Automatically requires tqdm and sequel

# In-memory database for demonstration purposes
DB = Sequel.sqlite
DB.create_table :items do
  primary_key :id
  Float :price
end

# Show progress during big inserts (this isn't new)
(0..100000).with_progress.each { DB[:items].insert(price: rand * 100) }

# Show progress during long SELECT queries
DB[:items].where{ price > 10 }.with_progress.each { |row| "do some processing here" }

TODO

  1. Performance improvements
  2. Add benchmark suite, expand test coverage
  3. Add smoothing for speed estimates
  4. Support unicode output (smooth blocks)
  5. By default, resize to the apparent width of the output terminal

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Issues· 2 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言