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

离线校验类蓝图、四类变量前缀、new/initialize 构造与实例方法调用

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

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


def case_class_blueprint_and_ruby_class_syntax():
    airplane = {"wheels": 3, "horsepower": 1000, "fuel_capacity_l": 100}
    car = {"wheels": 4, "horsepower": 200, "fuel_capacity_l": 25}
    class_name = "Customer"
    expected = {
        "camel_case_constant": True,
        "airplane_hp": 1000,
        "car_wheels": 4,
        "oop_pillars": 4,
    }
    actual = {
        "camel_case_constant": class_name[0].isupper() and class_name.isalnum(),
        "airplane_hp": airplane["horsepower"],
        "car_wheels": car["wheels"],
        "oop_pillars": len(["encapsulation", "abstraction", "polymorphism", "inheritance"]),
    }
    return {
        "name": "class_blueprint_and_ruby_class_syntax",
        "summary": "验证类蓝图与多实例属性区分、OOP 四大特性及 Ruby 类名首字母大写的常量命名规则",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_four_variable_scopes_in_class():
    def classify_var(name):
        if name.startswith("@@"):
            return "class_variable"
        if name.startswith("@"):
            return "instance_variable"
        if name.startswith("$"):
            return "global_variable"
        if name[0].islower() or name.startswith("_"):
            return "local_variable"
        return "constant"

    expected = {
        "id": "local_variable",
        "@cust_id": "instance_variable",
        "@@no_of_customers": "class_variable",
        "$global_cfg": "global_variable",
    }
    actual = {k: classify_var(k) for k in expected}
    return {
        "name": "four_variable_scopes_in_class",
        "summary": "校验 Ruby 类语境下局部变量、@实例变量、@@类变量与 $全局变量的词法前缀识别规则",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_new_and_initialize_object_construction():
    class Customer:
        no_of_customers = 0

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

    c1 = Customer("1", "John", "Wisdom Apartments, Ludhiya")
    c2 = Customer("2", "Poul", "New Empire road, Khandala")
    expected = {
        "c1_name": "John",
        "c2_name": "Poul",
        "isolated_instance_state": True,
        "shared_class_var": 0,
    }
    actual = {
        "c1_name": c1.cust_name,
        "c2_name": c2.cust_name,
        "isolated_instance_state": c1.cust_id != c2.cust_id,
        "shared_class_var": Customer.no_of_customers,
    }
    return {
        "name": "new_and_initialize_object_construction",
        "summary": "模拟 Customer.new 触发 initialize 并将局部参数绑定到独立对象实例变量 @cust_*",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_instance_method_definition_and_invocation():
    class Sample:
        def hello(self):
            return "Hello Ruby!"

    obj = Sample()
    expected = {"method_lowercase": True, "output": "Hello Ruby!"}
    actual = {"method_lowercase": "hello"[0].islower(), "output": obj.hello()}
    return {
        "name": "instance_method_definition_and_invocation",
        "summary": "验证类内 def 定义小写方法名、Sample.new 实例化及点号调用 object.hello 输出结果",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_class_blueprint_and_ruby_class_syntax(),
        case_four_variable_scopes_in_class(),
        case_new_and_initialize_object_construction(),
        case_instance_method_definition_and_invocation(),
    ]
    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-lab/explore-class.py

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

下载课程关联工程包:Ruby

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

下载课程关联工程包:Ruby

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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