蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 Customer 类实例变量插值、类变量共享计数与 main.rb 全量 8 行输出

PYTHONWindows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。explore-class-case-study.py
explore-class-case-study.py135 行
"""Offline Ruby 3.x semantic verification lab for Chapter 12 (class-case-study)."""
import json

VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"


class CustomerSim:
    no_of_customers = 0

    def __init__(self, cid, name, addr):
        self.cust_id = cid
        self.cust_name = name
        self.cust_addr = addr

    def display_details(self):
        return [
            f"Customer id {self.cust_id}",
            f"Customer name {self.cust_name}",
            f"Customer address {self.cust_addr}",
        ]

    def total_no_of_customers(self):
        CustomerSim.no_of_customers += 1
        return f"Total number of customers: {CustomerSim.no_of_customers}"


def case_customer_initialize_and_instance_interpolation():
    c1 = CustomerSim("1", "John", "Wisdom Apartments, Ludhiya")
    lines = c1.display_details()
    expected = {
        "lines": [
            "Customer id 1",
            "Customer name John",
            "Customer address Wisdom Apartments, Ludhiya",
        ],
        "short_ivar_interp_requires_double_quotes": True,
    }
    actual = {
        "lines": lines,
        "short_ivar_interp_requires_double_quotes": True,
    }
    return {
        "name": "customer_initialize_and_instance_interpolation",
        "summary": "校验 Customer#initialize 实例变量赋值及双引号内 #@cust_id 简写插值输出",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_class_variable_shared_counter():
    CustomerSim.no_of_customers = 0
    c1 = CustomerSim("1", "John", "Wisdom Apartments, Ludhiya")
    c2 = CustomerSim("2", "Poul", "New Empire road, Khandala")
    r1 = c1.total_no_of_customers()
    r2 = c2.total_no_of_customers()
    expected = {
        "first_call": "Total number of customers: 1",
        "second_call": "Total number of customers: 2",
        "final_counter": 2,
    }
    actual = {"first_call": r1, "second_call": r2, "final_counter": CustomerSim.no_of_customers}
    return {
        "name": "class_variable_shared_counter",
        "summary": "验证不同对象调用 total_no_of_customers 时共享递增同一类变量 @@no_of_customers",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_multi_instance_method_dispatch():
    CustomerSim.no_of_customers = 0
    c1 = CustomerSim("1", "John", "Wisdom Apartments, Ludhiya")
    c2 = CustomerSim("2", "Poul", "New Empire road, Khandala")
    expected = {
        "c1_id_line": "Customer id 1",
        "c2_id_line": "Customer id 2",
        "distinct_receivers": True,
    }
    actual = {
        "c1_id_line": c1.display_details()[0],
        "c2_id_line": c2.display_details()[0],
        "distinct_receivers": c1 is not c2,
    }
    return {
        "name": "multi_instance_method_dispatch",
        "summary": "校验点号消息分发在不同实例接收者 cust1 与 cust2 上读取各自隔离的实例变量",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_main_rb_full_stdout_trace():
    CustomerSim.no_of_customers = 0
    c1 = CustomerSim("1", "John", "Wisdom Apartments, Ludhiya")
    c2 = CustomerSim("2", "Poul", "New Empire road, Khandala")
    out = []
    out.extend(c1.display_details())
    out.append(c1.total_no_of_customers())
    out.extend(c2.display_details())
    out.append(c2.total_no_of_customers())
    expected = [
        "Customer id 1",
        "Customer name John",
        "Customer address Wisdom Apartments, Ludhiya",
        "Total number of customers: 1",
        "Customer id 2",
        "Customer name Poul",
        "Customer address New Empire road, Khandala",
        "Total number of customers: 2",
    ]
    return {
        "name": "main_rb_full_stdout_trace",
        "summary": "端到端核对完整 main.rb 示例的全部 8 行标准输出结果",
        "expected": expected,
        "actual": out,
        "passed": out == expected,
    }


def main():
    cases = [
        case_customer_initialize_and_instance_interpolation(),
        case_class_variable_shared_counter(),
        case_multi_instance_method_dispatch(),
        case_main_rb_full_stdout_trace(),
    ]
    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-class-case-study-lab/explore-class-case-study.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-class-case-study.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Ruby

课程关联工程包(登记关联45章) · 含当前章节或实例 · mbskycom202612a2434.zip

下载课程关联工程包:Ruby

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2441.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习