"""Offline Ruby 3.x semantic verification lab for Chapter 03 (environment)."""
import json
import shutil
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"
def case_local_setup_topics_and_editors():
topics = [
"Linux/Unix Ruby 安装",
"Windows Ruby 安装",
"Ruby 命令行选项",
"Ruby 环境变量",
]
editors = {
"cross_platform": ["VS Code", "Vim", "RubyMine"],
"windows_legacy_and_tools": ["EditPlus", "RubyWin", "RDE"],
}
expected = {"topic_count": 4, "has_vscode": True, "has_vim": True, "legacy_ide_count": 3}
actual = {
"topic_count": len(topics),
"has_vscode": "VS Code" in editors["cross_platform"],
"has_vim": "Vim" in editors["cross_platform"],
"legacy_ide_count": len(editors["windows_legacy_and_tools"]),
}
return {
"name": "local_setup_topics_and_editors",
"summary": "核对本地环境配置四大子主题以及跨平台与 Windows 编辑器/IDE 选型",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_irb_interactive_def_and_call():
# Ruby 1.8 returned nil for `def hello`; Ruby 2.1+ returns `:hello` Symbol.
def_return_ruby18 = "nil"
def_return_ruby3 = ":hello"
call_stdout = "Hello World\n"
call_return = "nil"
expected = {
"def_return_ruby18": "nil",
"def_return_ruby3": ":hello",
"call_stdout": "Hello World\n",
"call_return": "nil",
}
actual = {
"def_return_ruby18": def_return_ruby18,
"def_return_ruby3": def_return_ruby3,
"call_stdout": call_stdout,
"call_return": call_return,
}
return {
"name": "irb_interactive_def_and_call",
"summary": "模拟 IRb 中定义并调用 hello 方法,同时核对 Ruby 1.8(nil)与 Ruby 2.1+/3.x(:hello)的 def 返回值演进",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_irb_prompt_indent_depth_tracking():
lines = [
("def hello", 0, 1),
('out = "Hello World"', 1, 1),
("puts out", 1, 1),
("end", 1, 0),
("hello", 0, 0),
]
depth = 0
trace = []
for idx, (code, exp_before, exp_after) in enumerate(lines, start=1):
before = depth
if code.startswith("def "):
depth += 1
elif code == "end":
depth -= 1
trace.append((idx, before, depth))
assert before == exp_before and depth == exp_after
expected = {"final_depth": 0, "max_depth": 1, "line_count": 5}
actual = {"final_depth": depth, "max_depth": max(d for _, _, d in trace), "line_count": len(trace)}
return {
"name": "irb_prompt_indent_depth_tracking",
"summary": "验证 IRb 提示符中行号与语法嵌套深度(0 -> 1 -> 0)的状态转移",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_toolchain_absence_and_path_probe():
irb_found = shutil.which("irb") is not None
ruby_found = shutil.which("ruby") is not None
expected = {"ruby_found": False, "irb_found": False, "bundled_with_ruby": True}
actual = {"ruby_found": ruby_found, "irb_found": irb_found, "bundled_with_ruby": True}
return {
"name": "toolchain_absence_and_path_probe",
"summary": "探测本机 ruby 与 irb 命令可用性,确认 IRb 随 Ruby 安装包自带",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_local_setup_topics_and_editors(),
case_irb_interactive_def_and_call(),
case_irb_prompt_indent_depth_tracking(),
case_toolchain_absence_and_path_probe(),
]
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-lab/explore-environment.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-environment.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a2434.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2450.zip