"""Offline Ruby 3.x semantic verification lab for Chapter 14 (operator)."""
import json
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"
def spaceship(a, b):
return -1 if a < b else (1 if a > b else 0)
def case_arithmetic_comparison_spaceship_and_case_equality():
a, b = 10, 20
arith = {
"add": a + b,
"sub": a - b,
"mul": a * b,
"div": b // a,
"mod": b % a,
"pow": a ** 3,
}
cmp_res = {
"spaceship_lt": spaceship(a, b),
"spaceship_eq": spaceship(a, 10),
"spaceship_gt": spaceship(b, a),
"range_case_eq": 5 in range(1, 10),
"val_eq_int_float": (1 == 1.0),
"eql_same_type_int_float": (type(1) is type(1.0) and 1 == 1.0),
}
obj_a = [1, 2]
obj_b = [1, 2]
cmp_res["equal_same_id"] = obj_a is obj_a
cmp_res["equal_diff_copy"] = obj_a is obj_b
expected = {
"arith": {"add": 30, "sub": -10, "mul": 200, "div": 2, "mod": 0, "pow": 1000},
"cmp": {
"spaceship_lt": -1,
"spaceship_eq": 0,
"spaceship_gt": 1,
"range_case_eq": True,
"val_eq_int_float": True,
"eql_same_type_int_float": False,
"equal_same_id": True,
"equal_diff_copy": False,
},
}
actual = {"arith": arith, "cmp": cmp_res}
return {
"name": "arithmetic_comparison_spaceship_and_case_equality",
"summary": "校验算术运算、联合比较 <=>, 范围 case 相等 === 及 == / eql? / equal? 三种相等语义区分",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_assignment_parallel_swap_and_bitwise():
x, y, z = 10, 20, 30
x, y = y, z
a, b = 60, 13
expected = {
"parallel_after": [20, 30, 30],
"and": 12,
"or": 61,
"xor": 49,
"invert": -61,
"lshift": 240,
"rshift": 15,
}
actual = {
"parallel_after": [x, y, z],
"and": a & b,
"or": a | b,
"xor": a ^ b,
"invert": ~a,
"lshift": a << 2,
"rshift": a >> 2,
}
return {
"name": "assignment_parallel_swap_and_bitwise",
"summary": "验证并行赋值交换与 6 种按位运算(&, |, ^, ~, <<, >>)的精确补码计算结果",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_logical_short_circuit_ternary_and_range():
# In Ruby, `and`/`or` have lower precedence than `=` while `&&`/`||` have higher precedence than `=`.
# x = true && false -> x == false; x = true and false -> (x = true) and false -> x == true
val_amp = True and False
val_word_assign = True
ternary = "yes" if (10 < 20) else "no"
expected = {
"amp_assign": False,
"word_and_assign": True,
"ternary": "yes",
"inclusive_len": 10,
"exclusive_len": 9,
}
actual = {
"amp_assign": val_amp,
"word_and_assign": val_word_assign,
"ternary": ternary,
"inclusive_len": len(list(range(1, 11))),
"exclusive_len": len(list(range(1, 10))),
}
return {
"name": "logical_short_circuit_ternary_and_range",
"summary": "校验 &&/|| 与 and/or 相对于赋值运算符的优先级差异、三元表达式及 .. 与 ... 范围长度",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_defined_operator_double_colon_and_precedence():
defined_map = {
"foo": "local-variable",
"$_": "global-variable",
"bar": None,
"puts": "method",
"super_in_subclass": "super",
"yield_with_block": "yield",
}
# Simulate MR_COUNT & CONST :: resolution
root_const = {"MR_COUNT": 0, "CONST": " out there"}
foo_const = {"MR_COUNT": 0}
root_const["MR_COUNT"] = 1
foo_const["MR_COUNT"] = 2
inside_one_const = lambda: " in there"
inside_two_const = " inside two"
outputs = [
root_const["CONST"] + " inside one",
inside_two_const,
root_const["CONST"] + inside_two_const,
inside_two_const + root_const["CONST"],
inside_one_const() + inside_two_const,
]
expected = {
"defined_foo": "local-variable",
"defined_bar": None,
"root_mr_count": 1,
"foo_mr_count": 2,
"const_outputs": [
" out there inside one",
" inside two",
" out there inside two",
" inside two out there",
" in there inside two",
],
}
actual = {
"defined_foo": defined_map["foo"],
"defined_bar": defined_map["bar"],
"root_mr_count": root_const["MR_COUNT"],
"foo_mr_count": foo_const["MR_COUNT"],
"const_outputs": outputs,
}
return {
"name": "defined_operator_double_colon_and_precedence",
"summary": "校验 defined? 描述返回值以及顶层 ::CONST 与类/模块命名空间常量的解析次序",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_arithmetic_comparison_spaceship_and_case_equality(),
case_assignment_parallel_swap_and_bitwise(),
case_logical_short_circuit_ternary_and_range(),
case_defined_operator_double_colon_and_precedence(),
]
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-operator-lab/explore-operator.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-operator.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a2434.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2467.zip