Ruby 安装 - Windows
介绍 Windows 平台使用 RubyInstaller 安装 Ruby、向导路径选项、手动配置 Path 与 PATHEXT 环境变量以及命令行版本校验方法。
学完这一章,你能够
- 介绍 Windows 平台使用 RubyInstaller 安装 Ruby、向导路径选项、手动配置 Path 与 PATHEXT 环境变量以及命令行版本校验方法。
下载并运行 RubyInstaller 安装向导
在 Windows 环境下部署 Ruby,官方推荐使用开源项目 RubyInstaller(官网 `https://rubyinstaller.org/downloads/`,国内用户可访问镜像站 `https://rubyinstaller.cn/`)。下载页面会同时列出多个活跃分支(建议选择带有 MSYS2 Devkit 的 Ruby 3.x 64 位安装包,以便编译原生 C 扩展 gem)。
获取安装包后(早期示例为 `rubyinstaller-2.2.3.exe`,现代为 `rubyinstaller-devkit-3.x-x64.exe`),若为压缩包先解压到目标目录,双击可执行安装程序启动向导。按提示点击 Next,务必勾选 `Add Ruby executables to your PATH` 选项直至安装结束,让系统自动登记可执行文件路径。
手动配置 Path 与 PATHEXT 及验证安装
若安装时未勾选自动加入 PATH,可手动配置系统环境变量:在现代 Windows(以及历史上的 Windows NT/2000)中,打开“控制面板 → 系统属性 → 环境变量”,在系统变量中编辑 `Path`,将 Ruby 的 `bin` 目录追加至末尾并以分号分隔;随后编辑 `PATHEXT`,追加 `.RB` 与 `.RBW` 后确定保存(历史上的 Windows 9x 则是向 `c:\autoexec.bat` 写入 `set PATH=...`)。
配置完成后重新打开命令提示符或 PowerShell,执行 `ruby -v`。若终端正确输出已安装的 Ruby 版本号(例如早期示例 `ruby 2.2.3` 或现代 `ruby 3.3.x [x64-mingw-ucrt]`),即表明 Windows 下的 Ruby 解释器已就绪。
离线校验 Windows RubyInstaller 向导选项、Path/PATHEXT 拼接与 ruby -v 解析
"""Offline Ruby 3.x semantic verification lab for Chapter 05 (installation-windows)."""
import json
import re
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"
def case_windows_rubyinstaller_download_and_wizard():
urls = {
"official": "https://rubyinstaller.org/downloads/",
"cn_mirror": "https://rubyinstaller.cn/",
}
wizard_options = {"add_to_path": True, "associate_rb_rbw": True, "install_msys2_devkit": True}
expected = {"has_official": True, "has_cn_mirror": True, "path_checked": True}
actual = {
"has_official": "rubyinstaller.org" in urls["official"],
"has_cn_mirror": "rubyinstaller.cn" in urls["cn_mirror"],
"path_checked": wizard_options["add_to_path"],
}
return {
"name": "windows_rubyinstaller_download_and_wizard",
"summary": "核对 Windows RubyInstaller 下载渠道及向导中勾选 Add Ruby executables to your PATH 的关键选项",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_windows_path_and_pathext_configuration():
path_entries = [r"C:\Windows\System32", r"C:\Windows"]
ruby_bin = r"C:\Ruby33-x64\bin"
if ruby_bin not in path_entries:
path_entries.append(ruby_bin)
pathext = [".COM", ".EXE", ".BAT", ".CMD"]
for ext in (".RB", ".RBW"):
if ext not in pathext:
pathext.append(ext)
expected = {
"path_str": r"C:\Windows\System32;C:\Windows;C:\Ruby33-x64\bin",
"pathext_has_rb": True,
"pathext_has_rbw": True,
}
actual = {
"path_str": ";".join(path_entries),
"pathext_has_rb": ".RB" in pathext,
"pathext_has_rbw": ".RBW" in pathext,
}
return {
"name": "windows_path_and_pathext_configuration",
"summary": "验证 Windows 环境变量 Path 追加 bin 目录及 PATHEXT 追加 .RB 与 .RBW 的分号拼接格式",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_legacy_win9x_vs_nt_env_config():
win9x_line = r'set PATH="D:\Ruby\bin;%PATH%"'
nt_flow = ["控制面板/系统属性", "环境变量", "编辑系统变量 Path", "编辑系统变量 PATHEXT"]
expected = {"win9x_uses_autoexec": True, "nt_steps": 4}
actual = {"win9x_uses_autoexec": win9x_line.startswith("set PATH="), "nt_steps": len(nt_flow)}
return {
"name": "legacy_win9x_vs_nt_env_config",
"summary": "区分历史 Windows 9x(autoexec.bat)与 Windows NT/现代 Windows 系统环境变量配置流程",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_windows_cli_version_verification():
samples = [
"ruby 2.2.3p173 (2015-08-18 revision 51636) [x64-mingw32]",
"ruby 3.3.6 (2024-11-05 revision 75015d4c1f) [x64-mingw-ucrt]",
]
parsed = [re.match(r"^ruby\s+(\d+\.\d+\.\d+)", s).group(1) for s in samples]
expected = {"parsed_versions": ["2.2.3", "3.3.6"], "all_valid": True}
actual = {"parsed_versions": parsed, "all_valid": len(parsed) == 2}
return {
"name": "windows_cli_version_verification",
"summary": "校验 ruby -v 输出格式解析,对照历史 2.2.3 与现代 3.3.x UCRT 工具链版本标识",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_windows_rubyinstaller_download_and_wizard(),
case_windows_path_and_pathext_configuration(),
case_legacy_win9x_vs_nt_env_config(),
case_windows_cli_version_verification(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 离线验证器确认下载入口、分号分隔的 Path 与 .RB/.RBW PATHEXT 配置、历史 Win9x 与 NT 流程区别及 ruby -v 版本输出格式解析全部通过。
所需环境:Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。
当前源码 SHA256:407c462bf28e288f74ab3906bcd8f36431121d9c08c5aab35cacf0f0bed41027
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。
ZIP SHA256:dcc22d301d2fb5a2943bb05e3caf9dfcc3cd9dd43b3766caec383a81ba500b52
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在 Windows 系统中,将 `.RB` 和 `.RBW` 加入 `PATHEXT` 环境变量有什么作用?
展开参考解答
`PATHEXT` 定义了命令行可直接作为可执行程序调用的文件扩展名;加入 `.RB` 和 `.RBW` 并配合文件关联后,在终端中输入脚本名即可省略扩展名直接通过 Ruby 启动。