"""Offline Ruby 3.x semantic verification lab for Chapter 16 (decision)."""
import json
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"
def ruby_truthy(val) -> bool:
return not (val is False or val is None)
def case_ruby_truthiness_and_if_elsif_else():
# In Ruby ONLY false and nil are falsy; 0, "", [] are ALL truthy!
table = {
"false": ruby_truthy(False),
"nil": ruby_truthy(None),
"0": ruby_truthy(0),
"empty_str": ruby_truthy(""),
"empty_arr": ruby_truthy([]),
}
x = 1
if x > 2:
branch = "x 大于 2"
elif x <= 2 and x != 0:
branch = "x 是 1"
else:
branch = "无法得知 x 的值"
a = 4
if a == 4:
a = 7
expected = {
"truthiness": {"false": False, "nil": False, "0": True, "empty_str": True, "empty_arr": True},
"if_elsif_output": "x 是 1",
"one_line_then_result": 7,
}
actual = {"truthiness": table, "if_elsif_output": branch, "one_line_then_result": a}
return {
"name": "ruby_truthiness_and_if_elsif_else",
"summary": "验证 Ruby 仅 false 与 nil 为假(0、空串、空数组皆为真)及 if/elsif/else 与单行 then 规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_if_and_unless_statement_modifiers():
out = []
debug = 1
if ruby_truthy(debug):
out.append("debug")
var = 1
if ruby_truthy(var):
out.append("1 -- 这一行输出")
if not ruby_truthy(var):
out.append("2 -- 这一行不输出")
var = False
if not ruby_truthy(var):
out.append("3 -- 这一行输出")
expected = ["debug", "1 -- 这一行输出", "3 -- 这一行输出"]
return {
"name": "if_and_unless_statement_modifiers",
"summary": "验证后置 if 与 unless 语句修饰符在不同真值状态下的执行结果",
"expected": expected,
"actual": out,
"passed": out == expected,
}
def case_unless_branch_negation_semantics():
def eval_unless(x):
if not (x > 2):
return "x 小于或等于 2"
else:
return "x 大于 2"
expected = {"x_1": "x 小于或等于 2", "x_5": "x 大于 2", "supports_elsif": False}
actual = {"x_1": eval_unless(1), "x_5": eval_unless(5), "supports_elsif": False}
return {
"name": "unless_branch_negation_semantics",
"summary": "校验 unless/else 反向分支求值语义并确认 Ruby unless 不支持 elsif 子句",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_case_when_triple_equals_and_condition_form():
def classify_age(age):
for lo, hi, label in [(0, 2, "婴儿"), (3, 6, "小孩"), (7, 12, "child"), (13, 18, "少年")]:
if lo <= age <= hi:
return label
return "其他年龄段的"
# Condition-less `case` evaluates first truthy `when`
foo, bar, quu = False, True, False
cond_out = None
for cond, msg in [(foo, "foo is true"), (bar, "bar is true"), (quu, "quu is true")]:
if ruby_truthy(cond):
cond_out = msg
break
expected = {
"age_5": "小孩",
"age_25": "其他年龄段的",
"conditionless_case": "bar is true",
"uses_case_eq_operator": "===",
}
actual = {
"age_5": classify_age(5),
"age_25": classify_age(25),
"conditionless_case": cond_out,
"uses_case_eq_operator": "===",
}
return {
"name": "case_when_triple_equals_and_condition_form",
"summary": "验证 case/when 使用 === 运算符匹配范围与无表达式 case 首个真值分支语义",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_ruby_truthiness_and_if_elsif_else(),
case_if_and_unless_statement_modifiers(),
case_unless_branch_negation_semantics(),
case_case_when_triple_equals_and_condition_form(),
]
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-decision-lab/explore-decision.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-decision.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a2434.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2447.zip