"""Offline Ruby 3.x semantic verification lab for Chapter 08 (environment-variables)."""
import json
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"
def case_rubylib_and_prefix_path_resolution():
posix_lib = "/usr/lib/ruby:/opt/app/lib".split(":")
win_lib = r"C:\Ruby33\lib;D:\work\lib".split(";")
prefix_from, prefix_to = r"C:\Ruby33", r"E:\PortableRuby"
rewritten = [p.replace(prefix_from, prefix_to, 1) if p.startswith(prefix_from) else p for p in win_lib]
expected = {
"posix_count": 2,
"win_rewritten_first": r"E:\PortableRuby\lib",
"win_second_preserved": r"D:\work\lib",
}
actual = {
"posix_count": len(posix_lib),
"win_rewritten_first": rewritten[0],
"win_second_preserved": rewritten[1],
}
return {
"name": "rubylib_and_prefix_path_resolution",
"summary": "校验 RUBYLIB 在 POSIX 冒号与 Windows 分号下的拆分以及 RUBYLIB_PREFIX 前缀替换规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_rubyopt_and_rubypath_precedence():
env = {"RUBYPATH": "/opt/ruby-scripts", "PATH": "/usr/local/bin:/usr/bin", "RUBYOPT": "-w -I./lib"}
s_search_order = env["RUBYPATH"].split(":") + env["PATH"].split(":")
opt_tokens = env["RUBYOPT"].split()
expected = {
"first_search_dir": "/opt/ruby-scripts",
"rubypath_beats_path": True,
"rubyopt_tokens": ["-w", "-I./lib"],
}
actual = {
"first_search_dir": s_search_order[0],
"rubypath_beats_path": s_search_order.index("/opt/ruby-scripts") < s_search_order.index("/usr/local/bin"),
"rubyopt_tokens": opt_tokens,
}
return {
"name": "rubyopt_and_rubypath_precedence",
"summary": "验证 -S 搜索时 RUBYPATH 优先于 PATH 以及 RUBYOPT 自动注入解释器选项的规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_home_logdir_and_rubyshell_fallbacks():
def resolve_chdir(env):
return env.get("HOME") or env.get("LOGDIR")
def resolve_shell(env):
return env.get("RUBYSHELL") or env.get("SHELL") or env.get("COMSPEC")
expected = {
"chdir_with_home": "/home/alice",
"chdir_fallback_logdir": "/usr/log/ruby",
"shell_priority": "/bin/zsh",
"shell_win_fallback": r"C:\Windows\System32\cmd.exe",
}
actual = {
"chdir_with_home": resolve_chdir({"HOME": "/home/alice", "LOGDIR": "/usr/log/ruby"}),
"chdir_fallback_logdir": resolve_chdir({"LOGDIR": "/usr/log/ruby"}),
"shell_priority": resolve_shell({"RUBYSHELL": "/bin/zsh", "SHELL": "/bin/bash"}),
"shell_win_fallback": resolve_shell({"COMSPEC": r"C:\Windows\System32\cmd.exe"}),
}
return {
"name": "home_logdir_and_rubyshell_fallbacks",
"summary": "校验 Dir.chdir 无参时的 HOME/LOGDIR 回退链以及 RUBYSHELL/SHELL/COMSPEC 优先级",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_env_object_and_unix_env_dump_parsing():
dump_text = "\n".join([
"RUBYPATH=/usr/bin",
"SHELL=/bin/bash",
"HOME=/root",
"LOGDIR=/usr/log/ruby",
"RUBYLIB=/usr/lib/ruby",
"PATH=/usr/local/bin:/bin:/usr/bin",
])
parsed = dict(line.split("=", 1) for line in dump_text.splitlines() if "=" in line)
expected = {
"keys_count": 6,
"rubylib": "/usr/lib/ruby",
"rubypath": "/usr/bin",
"home": "/root",
}
actual = {
"keys_count": len(parsed),
"rubylib": parsed["RUBYLIB"],
"rubypath": parsed["RUBYPATH"],
"home": parsed["HOME"],
}
return {
"name": "env_object_and_unix_env_dump_parsing",
"summary": "解析 Unix env 输出并核对 Ruby ENV 对象以键值对映射访问关键运行环境变量",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_rubylib_and_prefix_path_resolution(),
case_rubyopt_and_rubypath_precedence(),
case_home_logdir_and_rubyshell_fallbacks(),
case_env_object_and_unix_env_dump_parsing(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。
入口文件:projects/ruby-environment-variables-lab/explore-environment-variables.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-environment-variables.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a2434.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2451.zip