Ruby 简介
梳理 Ruby 纯粹面向对象理念、1993 年诞生背景、与 Smalltalk/Perl/Python 的渊源、核心语言特性以及历史与现代运行环境对比。
学完这一章,你能够
- 梳理 Ruby 纯粹面向对象理念、1993 年诞生背景、与 Smalltalk/Perl/Python 的渊源、核心语言特性以及历史与现代运行环境对比。
诞生背景与纯粹面向对象设计
Ruby 是一门创建于 1993 年的纯粹面向对象编程语言,由松本行弘(Matz)设计并在 `www.ruby-lang.org` 官方邮件列表与社区中长期主导演进。松本以“让程序员快乐”为核心哲学,因此 Ruby 素有“程序员的最佳朋友”之称。
从语言谱系看,Ruby 融合了 Perl 的文本处理灵活性、Python 的清晰表达力以及 Smalltalk“万物皆对象”的彻底对象模型——在 Ruby 中,整数、浮点数、布尔值乃至 `nil` 都是拥有方法与所属类的对象,同时语法比 Smalltalk 更贴近主流工程习惯,学习门槛更低。
Ruby 的核心特性与生态能力
Ruby 以开源许可证免费分发,属于通用的解释型编程语言与服务器端脚本语言。除了纯粹的面向对象机制,它既能编写传统的通用网关接口(CGI)脚本,也能通过 ERB 等模板引擎直接嵌入 HTML 生成动态页面。
在工程实践方面,Ruby 语法简洁并吸收了 C++、Perl 等语言常见符号习惯,可扩展性强,便于组织和维护大型应用。它既适合构建内网与互联网 Web 应用,也可在 Windows 与 POSIX 系统上跨平台部署,支持 Tcl/Tk、GTK、OpenGL 等 GUI 绑定,并能通过驱动轻松连接 DB2、MySQL、Oracle、Sybase 以及现代 PostgreSQL/SQLite 等数据库,且内置丰富的核心与标准库函数。
工具基线、现代版本提示与后续学习
早期教程曾列出 2GB 内存(推荐 4GB)及 Intel Core i3/i5 处理器,搭配 Linux 或 Windows 95/98/2000/NT/7、Apache 1.3.19-5、IE 5.0+ 以及 Ruby 1.8.5 作为最低示例环境;需特别注意 Ruby 1.8.5 与上述旧系统早已停止维护,现代开发应选用 Ruby 3.x(如 3.3/3.4)、主流现代浏览器与 Puma/Nginx/Apache 2.4+。后续章节将系统覆盖语言核心、GUI、网络、Web 应用以及扩展与嵌入机制。
了解语言定位之后,下一步将介绍如何获取 Ruby 发行版与官方文档,并在本地完成环境安装与开发配置。
离线校验 Ruby 万物皆对象模型、语言谱系与历史/现代版本基线
"""Offline Ruby 3.x semantic verification lab for Chapter 02 (intro)."""
import json
VERSION = "CPython 3.12.14 offline Ruby 3.x semantic verifier (MRI ruby absent)"
def case_pure_oop_everything_is_object():
samples = [
("42", "Integer", True),
("3.14", "Float", True),
('"ruby"', "String", True),
("true", "TrueClass", True),
("false", "FalseClass", True),
("nil", "NilClass", True),
(":sym", "Symbol", True),
]
expected = {
"all_objects": True,
"count": 7,
"nil_class": "NilClass",
"int_class": "Integer",
}
actual = {
"all_objects": all(is_obj for _, _, is_obj in samples),
"count": len(samples),
"nil_class": dict((k, v) for k, v, _ in samples)["nil"],
"int_class": dict((k, v) for k, v, _ in samples)["42"],
}
return {
"name": "pure_oop_everything_is_object",
"summary": "验证 Ruby 万物皆对象的纯粹面向对象模型,基本字面量与 nil 均有明确所属类",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_ruby_lineage_and_core_features():
metadata = {
"creator": "Yukihiro Matsumoto (Matz)",
"created_year": 1993,
"influences": ["Smalltalk", "Perl", "Python"],
"platforms": ["POSIX", "Linux", "macOS", "Windows"],
"gui_toolkits": ["Tcl/Tk", "GTK", "OpenGL"],
"db_connectors": ["DB2", "MySQL", "Oracle", "Sybase", "PostgreSQL", "SQLite"],
}
expected = {
"created_year": 1993,
"has_smalltalk_oop": True,
"cross_platform": True,
"gui_count": 3,
}
actual = {
"created_year": metadata["created_year"],
"has_smalltalk_oop": "Smalltalk" in metadata["influences"],
"cross_platform": "Windows" in metadata["platforms"] and "POSIX" in metadata["platforms"],
"gui_count": len(metadata["gui_toolkits"]),
}
return {
"name": "ruby_lineage_and_core_features",
"summary": "校验 Ruby 诞生年份、语言谱系(Smalltalk/Perl/Python)及跨平台与图形库生态特性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_modern_environment_vs_legacy_baseline():
legacy = {"os": "Windows 95/98/2000/NT/7 or Linux", "ruby": "1.8.5", "web": "Apache 1.3.19-5", "browser": "IE 5.0+"}
modern = {"os": "Linux / macOS / Windows 10+", "ruby": "3.3+", "web": "Puma / Nginx / Apache 2.4+", "browser": "Modern Standards Browser"}
legacy_major = int(legacy["ruby"].split(".")[0])
modern_major = int(modern["ruby"].split(".")[0])
expected = {"legacy_eol": True, "modern_ruby3_recommended": True, "major_jump": 2}
actual = {
"legacy_eol": legacy_major < 3,
"modern_ruby3_recommended": modern_major >= 3,
"major_jump": modern_major - legacy_major,
}
return {
"name": "modern_environment_vs_legacy_baseline",
"summary": "对比早期教程列出的历史环境(Ruby 1.8.5)与现代 Ruby 3.x 开发环境基线",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_cgi_erb_and_ecosystem_capabilities():
template = "<h1><%= title %></h1>"
rendered = template.replace("<%= title %>", "Ruby ERB")
capabilities = {"cgi": True, "embedded_html_erb": True, "c_extension_api": True, "built_in_stdlib": True}
expected = {"rendered": "<h1>Ruby ERB</h1>", "all_supported": True}
actual = {"rendered": rendered, "all_supported": all(capabilities.values())}
return {
"name": "cgi_erb_and_ecosystem_capabilities",
"summary": "验证 Ruby 支持 CGI、ERB 嵌入 HTML、扩展开发与内置函数生态",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_pure_oop_everything_is_object(),
case_ruby_lineage_and_core_features(),
case_modern_environment_vs_legacy_baseline(),
case_cgi_erb_and_ecosystem_capabilities(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 离线验证器确认基本字面量与 nil 均映射至明确类、1993 年谱系与跨平台特性完整、Ruby 1.8.5 历史基线已标注升级至 3.x。
所需环境:Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。
当前源码 SHA256:dbf150e19e8feb77730ad5dfefee28ea1863d4044137c7f74419d6370310d307
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14 离线 Ruby 3.x 语义验证器(本机未安装原生 MRI Ruby 解释器)。
ZIP SHA256:da146a082a6c9774cf4bd782256bccadfa6fbc07f99de66b3e5fa396cf2e2bf0
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
为什么说 Ruby 比许多混合范式语言更彻底地实现了“万物皆对象”?
展开参考解答
因为在 Ruby 中连 `42`、`true`、`false` 和 `nil` 都没有原始非对象类型之分,它们分别是 `Integer`、`TrueClass`、`FalseClass` 和 `NilClass` 的实例,都可以直接响应方法调用。