Ruby 命令行与运维命令详解
详解 Ruby 常用命令行与运维命令:ruby -v、gem list/install/uninstall、bundle install、irb、rake、ruby -e 与测试工具 minitest/test-unit,逐命令给出 Ubuntu 22.04 Ruby 3.0.2 真实回显与速查表
Ruby 命令行与运维命令详解
目标:系统梳理 Ruby 运行时的常用命令行与运维命令:
ruby(版本/执行/语法检查)、gem(list/install/uninstall/cleanup)、bundle(install/config)、irb、rake、测试工具(minitest/test-unit),逐命令给出真实回显与命令速查表。基于 Ubuntu 22.04(Ruby 3.0.2p107、RubyGems 3.3.5、Bundler 2.2.22)实测。
环境信息
- 操作系统:Ubuntu 22.04(x86_64)
- Ruby:3.0.2p107(2021-07-07 revision 0db68f0233)[x86_64-linux-gnu]
- RubyGems:3.3.5(
gem) - Bundler:2.2.22(Ubuntu 命令名
bundle3.0) - Rake:13.0.6
- irb:随 ruby 分发(1.3.5)
- 测试库:minitest 5.14.2、test-unit 3.3.7
工具概览
| 工具 | 命令 | 主要用途 |
|---|---|---|
| ruby | ruby | 解释执行、语法检查、版本查看 |
| RubyGems | gem | gem 安装/卸载/列表/源管理 |
| Bundler | bundle3.0 | 项目依赖管理 |
| irb | irb | 交互式解释器 |
| rake | rake | 任务脚本(构建/测试) |
| minitest / test-unit | ruby -Itest / ruby -Ilib | 单元测试 |
前置条件
- 已按《Ubuntu 22.04 安装 Ruby 环境指南》安装 Ruby 环境
- root 用户示例(密码 q1w2e3r4,仅演示)
1. ruby 命令
1.1 查看版本
root@host:~# ruby -v
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux-gnu]
1.2 直接执行脚本文件
root@host:~# echo 'puts 6*7' > /tmp/hello.rb
root@host:~# ruby /tmp/hello.rb
42
1.3 命令行内联执行(-e)
root@host:~# ruby -e 'require "json"; puts JSON.generate({a: 1, b: [2, 3]})'
{"a":1,"b":[2,3]}
root@host:~# ruby -e 'puts RUBY_VERSION'
3.0.2
1.4 语法检查(-c)与警告(-w)
root@host:~# ruby -c /tmp/hello.rb
Syntax OK
1.5 加载路径(-I)与 require 调试
ruby -I /opt/mylib -e 'require "mymod"'
ruby -e 'p $LOAD_PATH'
2. gem 命令
2.1 版本与源
root@host:~# gem -v
3.3.5
root@host:~# gem sources -l
*** CURRENT SOURCES ***
https://rubygems.org/
2.2 列出已装 gem
root@host:~# gem list
abbrev (default: 0.1.0)
base64 (default: 0.1.0)
bigdecimal (default: 3.0.0)
bundler (default: 2.2.22)
json (default: 2.5.1)
minitest (5.14.2)
rake (13.0.6, 13.0.3)
test-unit (3.3.7)
💡 说明:
default:前缀表示随 Ruby 发行版分发;rake (13.0.6, 13.0.3)表示装了两个版本,默认用 13.0.6。精确查找:gem list | grep minitest。
2.3 安装 gem
root@host:~# gem install colorize
Successfully installed colorize-1.1.0
Parsing documentation for colorize-1.1.0
Done installing documentation for colorize after 0 seconds
1 gem installed
💡 说明:
gem install json实测输出json (default: 2.5.1)已存在时会提示已装;安装指定版本:gem install colorize -v 1.1.0。
2.4 卸载与清理
gem uninstall colorize # 交互确认,或 -x 强制
gem cleanup # 清理多余旧版本
gem pristine colorize # 恢复被改动 gem 文件
2.5 查看 gem 详情与位置
root@host:~# gem which rake
/usr/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake.rb
root@host:~# gem env home
/var/lib/gems/3.0.0
3. bundle 命令(Bundler)
Ubuntu 22.04 上 bundler 可执行文件名为 bundle3.0:
root@host:~# bundle3.0 -v
Bundler version 2.2.22
项目初始化与依赖安装:
cd /opt/mysvc
bundle3.0 init # 生成 Gemfile
bundle3.0 add sinatra # 添加依赖并安装
bundle3.0 install # 按 Gemfile 安装(写 Gemfile.lock)
bundle3.0 exec ruby app.rb # 在项目 gem 环境内执行
bundle3.0 config list # 查看配置
bundle3.0 update sinatra # 更新指定 gem
💡 说明:生产部署常用
bundle3.0 install --deployment(或bundle3.0 config set --global deployment true)只使用 Gemfile.lock 并装入 vendor/bundle。
4. irb 命令
root@host:~# irb
irb(main):001:0> 2 + 3 * 4
=> 14
irb(main):002:0> "hello".upcase
=> "HELLO"
irb(main):003:0> exit
管道非交互执行:
root@host:~# echo 'puts 2+3*4' | irb
Switch to inspect mode.
2+3*4
14
5. rake 命令
root@host:~# rake -V
rake, version 13.0.6
root@host:~# rake -T # 列出任务(需在含 Rakefile 目录)
Rakefile 示例:
task :hello do
puts "Hello from rake"
end
root@host:/opt/mysvc# rake hello
Hello from rake
6. 测试工具(minitest / test-unit)
6.1 minitest(Ruby 自带)
# /tmp/test_math.rb
require "minitest/autorun"
class TestMath < Minitest::Test
def test_multiplication
assert_equal 42, 6 * 7
end
end
root@host:~# ruby /tmp/test_math.rb
Run options: --seed 27617
# Running:
.
Finished in 0.001907s, 524.4840 runs/s, 524.4840 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
6.2 test-unit
# /tmp/test_unit_math.rb
require "test-unit"
class TestMath < Test::Unit::TestCase
def test_multiplication
assert_equal 42, 6 * 7
end
end
root@host:~# ruby /tmp/test_unit_math.rb
Loaded suite /tmp/test_unit_math
Started
.
Finished in 0.000405804 seconds.
-------------------------------------------------------------------------------
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
-------------------------------------------------------------------------------
2464.24 tests/s, 2464.24 assertions/s
💡 说明:minitest 与 test-unit 均为 ruby-full 自带(实测版本 5.14.2 / 3.3.7),无需额外安装即可运行上述测试。
命令速查表
| 场景 | 命令 |
|---|---|
| 查看 Ruby 版本 | ruby -v |
| 执行脚本 | ruby /path/script.rb |
| 内联执行 | ruby -e 'puts 1+1' |
| 语法检查 | ruby -c /path/script.rb |
| 查看 gem 版本 | gem -v |
| 列出 gem | gem list |
| 安装 gem | gem install <gem名> [-v 版本] |
| 卸载 gem | gem uninstall <gem名> |
| 查看 gem 源 | gem sources -l |
| 添加/删除源 | gem sources --add <url> / gem sources --remove <url> |
| 查看 gem 位置 | gem which <gem名> |
| Bundler 版本 | bundle3.0 -v |
| 安装依赖 | bundle3.0 install |
| 进入 irb | irb |
| 列出 rake 任务 | rake -T |
| 跑 minitest | ruby /tmp/test_math.rb |
注意事项
- Ubuntu 22.04 的 bundler 命令是
bundle3.0而非bundle;若直接执行bundle报 command not found,用bundle3.0或gem install bundler。 gem install生产环境建议先配置~/.gemrc的install: --no-document(见配置详解篇),跳过 ri/rdoc 生成。- 多版本 ruby(rvm)环境下,
gem与ruby必须同版本,用which ruby/gem env home核对。
常见问题
问:bundle 命令找不到?
答:Ubuntu 用 bundle3.0;或 gem install bundler 后直接可用 bundle。
问:gem install 很慢或失败?
答:切国内源 gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/;或设置 HTTP_PROXY/HTTPS_PROXY 环境变量。
问:ruby app.rb 报 LoadError: cannot load such file?
答:目标库未安装(gem install <库名>)或未在加载路径;用 gem which <库名> 排查。
问:rake -T 空输出?
答:当前目录无 Rakefile,或任务未声明 desc(描述)导致不显示;rake -P 可查看全部任务(含无描述任务)。
参考链接
- Ruby 命令行:https://docs.ruby-lang.org/en/master/
- RubyGems 命令:https://guides.rubygems.org/command-reference/
- Bundler 命令:https://bundler.io/commands.html
- Rake:https://github.com/ruby/rake
- minitest:https://github.com/minitest/minitest