"""Offline Ruby 3.x semantic verification lab for Chapter 25 (range)."""
import json
import re
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"
def ruby_str_succ(s: str) -> str:
"""Ruby String#succ for ASCII lowercase letters."""
chars = list(s)
i = len(chars) - 1
while i >= 0:
if chars[i] < "z":
chars[i] = chr(ord(chars[i]) + 1)
return "".join(chars)
chars[i] = "a"
i -= 1
return "a" + "".join(chars)
def ruby_str_range(start: str, end: str, exclude_end: bool = False):
out = []
cur = start
while True:
if cur == end:
if not exclude_end:
out.append(cur)
break
out.append(cur)
cur = ruby_str_succ(cur)
if len(out) > 100:
break
return out
def case_inclusive_exclusive_sequences():
incl = list(range(1, 5 + 1))
excl = list(range(1, 5))
chars = ruby_str_range("a", "d")
words = ruby_str_range("bar", "bat")
expected = {
"incl_1_to_5": [1, 2, 3, 4, 5],
"excl_1_to_5": [1, 2, 3, 4],
"a_to_d": ["a", "b", "c", "d"],
"bar_to_bat": ["bar", "bas", "bat"],
}
actual = {
"incl_1_to_5": incl,
"excl_1_to_5": excl,
"a_to_d": chars,
"bar_to_bat": words,
}
return {
"name": "inclusive_exclusive_sequences",
"summary": "核验闭区间 .. 与半开区间 ... 端点差异,以及字符串范围基于 String#succ 的序列展开",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_range_enumeration_and_reject():
digits = list(range(0, 10))
rejected = [i for i in digits if not (i < 5)]
expected = {
"include_5": True,
"min": 0,
"max": 9,
"reject_lt_5": [5, 6, 7, 8, 9],
"each_count": 10,
}
actual = {
"include_5": 5 in digits,
"min": min(digits),
"max": max(digits),
"reject_lt_5": rejected,
"each_count": len(digits),
}
return {
"name": "range_enumeration_and_reject",
"summary": "核验 Range#include?、min、max、reject 与 each 遍历语义",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_case_and_flipflop_conditions():
def grade(score: int) -> str:
if 0 <= score <= 40:
return "糟糕的分数"
if 41 <= score <= 60:
return "快要及格"
if 61 <= score <= 70:
return "及格分数"
if 71 <= score <= 100:
return "良好分数"
return "错误的分数"
lines = ["intro", "start block", "middle line", "end block", "after"]
active = False
captured = []
for line in lines:
if not active and re.search(r"start", line):
active = True
if active:
captured.append(line)
if re.search(r"end", line):
active = False
expected = {
"score_70": "及格分数",
"score_85": "良好分数",
"score_105": "错误的分数",
"flipflop_lines": ["start block", "middle line", "end block"],
}
actual = {
"score_70": grade(70),
"score_85": grade(85),
"score_105": grade(105),
"flipflop_lines": captured,
}
return {
"name": "case_and_flipflop_conditions",
"summary": "核验 Range 在 case/when 区间匹配及 /start/../end/ 触发器条件中的状态切换",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_triple_equals_interval_check():
def range_covers(lo, hi, val, exclude_end=False):
return (lo <= val < hi) if exclude_end else (lo <= val <= hi)
expected = {
"int_in_1_10": True,
"char_c_in_a_j": True,
"char_z_in_a_j": False,
"excl_5_in_1_5": False,
}
actual = {
"int_in_1_10": range_covers(1, 10, 5),
"char_c_in_a_j": range_covers("a", "j", "c"),
"char_z_in_a_j": range_covers("a", "j", "z"),
"excl_5_in_1_5": range_covers(1, 5, 5, exclude_end=True),
}
return {
"name": "triple_equals_interval_check",
"summary": "核验 Range#=== 间隔包含检测在整型、字符与半开边界下的布尔结果",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_inclusive_exclusive_sequences(),
case_range_enumeration_and_reject(),
case_case_and_flipflop_conditions(),
case_triple_equals_interval_check(),
]
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-range-lab/explore-range.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-range.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装 MRI ruby 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a2434.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2468.zip