Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rb

> 编程语言
Open source

Turns Ruby into a versatile command line utility

1.5K stars0 likes0 views
WebsiteGitHub

About

Turns Ruby into a versatile command line utility

rb

With 9 lines of Ruby replace most of the command line tools that you use to process text inside of the terminal.

Here's the code

ruby
#!/usr/bin/env ruby
File.join(Dir.home, '.rbrc').tap { |f| load f if File.exist?(f) }

def execute(_, code)
  puts _.instance_eval(&code)
rescue Errno::EPIPE
  exit
end

single_line = ARGV.delete('-l')
code = eval("Proc.new { #{ARGV.join(' ')} }")
single_line ? STDIN.each { |l| execute(l.chomp, code) } : execute(STDIN.each_line, code)

Clone this repo and copy the rb file to somewhere in your path (or just copy and paste the above).

With this you can use ruby as a command line utility much more ergonomically than invoking it the standard way.

There's only one switch -l which runs your code on each line separately. Otherwise you get an Enumerator that returns the lines of stdin. It's instance_evaled so some methods need self to work, eg. self[-1]

Install

Just paste this line into your terminal to install rb:

bash
sudo curl https://raw.githubusercontent.com/thisredone/rb/master/rb -o /usr/local/bin/rb && sudo chmod +x /usr/local/bin/rb

Examples

Extract docker images from running containers
bash
> docker ps | rb drop 1 | rb -l split[1]

# ubuntu
# postgres
Display how much time ago containers have exited
bash
> docker ps -a | rb grep /Exited/ | rb -l 'split.last.ljust(20) + " => " + split(/ {2,}/)[-2]'

# angry_hamilton      => Exited (0) 18 hours ago
# dreamy_lamport      => Exited (0) 3 days ago
# prickly_hypatia     => Exited (0) 2 weeks ago
Sort df -h output by Use%
…
Count files by their extension
bash
> find . -type f | rb 'group_by(&File.method(:extname)).map { |ext, o| "#{ext.chomp}: #{o.size}" }'

# : 3
# .rb: 19
# .md: 1
This problem http://vegardstikbakke.com/unix/
ruby
ls | rb 'group_by { |x| x[/\d+/] }.select { |_, y| y.one? }.keys'

Extending rb

The ~/.rbrc file is loaded if it's available. Anything defined in there will be available inside rb scripts.

ruby
# ~/.rbrc

class String
  def black; "\033[30m#{self}\033[0m" end
  def red;   "\033[31m#{self}\033[0m" end
end
bash
> ls | rb first.red

Alternatives

For a more portable solution, note you can run ruby with the -e flag.

$< is a two-character global equivalent to ARGF. ARGF gives you access to the standard input and includes Enumerable.

bash
> docker ps | ruby -e '$<.drop(1).each{puts _1.split[1]}'

# ubuntu
# postgres
bash
> docker ps -a | ruby -e '$<.grep(/Exited/).each{puts _1.split.last.ljust(20) + " => " + _1.split(/ {2,}/)[-2]}'

# angry_hamilton      => Exited (0) 18 hours ago
# dreamy_lamport      => Exited (0) 3 days ago
# prickly_hypatia     => Exited (0) 2 weeks ago

Issues· 1 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

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