编程Ruby:实用程序员指南(第二版)
Ruby编程语言的全面参考与教程,涵盖基本语法、面向对象设计、反射和线程等高级特性,并提供1.8版本的完整库参考。
课程概述
📚 内容概要
一本全面的 Ruby 编程语言参考手册与教程,涵盖基础语法、面向对象设计、反射与线程等高级特性,并提供 1.8 版本的完整标准库参考。
掌握优雅而强大的编程艺术,尽在权威的 Ruby 语言指南。
作者: 戴夫·托马斯(Dave Thomas)、查德·福勒(Chad Fowler)、安迪·亨特(Andy Hunt)
致谢: 来自 Ruby 邮件列表的审阅者、松本行弘(Matz)、查德·福勒、金·温普塞特(Kim Wimpsett)以及《程序员修炼之道》社区成员。
🎯 学习目标
- 使用多种方法(二进制包、源码编译或 CVS)安装并配置 Ruby 环境,并使用交互式工具如 irb 和 ri。
- 应用 Ruby 的命名规范和面向对象原则来定义方法并管理变量。
- 使用基本数组、哈希和正则表达式操作数据。
- 使用单继承、super 和混入(mixins)定义并实现类层次结构。
- 使用访问控制(公共、私有、受保护)和属性机制控制对象可见性和数据完整性。
- 构建健壮的容器类(例如 SongList),并集成单元测试。
- 使用不同进制(十六进制、八进制、二进制)实现数值逻辑,并利用数值迭代器控制流程。
- 使用插值、此处文档(heredocs)及各种分隔符格式构造复杂字符串。
- 将范围(Ranges)作为条件逻辑中的“开关”或区间检测器使用。
- 使用灵活的参数列表(星号操作符)、基于哈希的“关键字”参数以及块到过程的转换来定义和调用方法。
课程 共 15 课时 · 预计 45.0h
概述: 本课涵盖分发 Ruby 代码和构建基于 Web 的应用程序的基本机制。内容包括使用 RubyGems 进行包管理与版本控制,通过 CGI 和模板系统实现网页接口,以及使用 SOAP 和网络服务集成分布式系统。
学习成果:
- 使用 Gem 规范和 Rake 定义并打包 Ruby 库。
- 使用 cgi.rb 开发动态网页应用,处理查询参数、Cookie 和会话。
- 实现模板系统(ERB、Amrita),实现应用逻辑与展示层分离。
概述: 本课探讨使用 Tk 库在 Ruby 中构建图形用户界面(GUI),从基础控件设置到高级画布管理和滚动。此外,还探讨 Windows 平台的特定集成,包括自动化性能基准测试,以及使用 DL 库直接调用 Windows DLL。
学习成果:
- 使用控件和多种几何管理器(pack、place、grid)构建功能性的 Ruby Tk 应用。
- 通过事件绑定和双向滚动通信实现交互元素。
- 将现有的 Perl/Tk 文档转换为有效的 Ruby 代码,并使用 DL 库进行低级别的 Windows API 调用。
概述: 本课探讨 Ruby 编程语言与 C 之间的接口,重点讲解如何使用 C 扩展来增强 Ruby 功能,以及如何将 Ruby 解释器嵌入 C 应用中。学生将学习 Ruby 对象的内部表示、包装 C 数据结构的协议,以及构建和配置这些扩展所需的工具。
学习成果:
- 在 C 环境中理解并操作 Ruby 的 VALUE 类型和即时对象。
- 实现自定义 C 支持的 Ruby 类的多步分配与初始化协议。
- 使用 mkmf 和 extconf.rb 自动化 Ruby 扩展的构建过程。
概述: 本课对 Ruby 语言规范进行技术性深度剖析,涵盖专业开发所需的基本构建模块。学生将探索 Ruby 基本数据类型的细微差别,变量与常量作用域的严格规则,以及表达式和方法参数的机制。
学习成果:
- 区分各种基本类型(整数、字符串、符号),并应用 %w 和 %q 等简写语法。
- 管理全局、类、实例和局部层级的变量作用域与常量可见性。
- 使用布尔表达式、case 语句和循环修饰符实现复杂的控制流程。
概述: 本课探讨 Ruby 通过方法调用、对象定义和错误处理执行代码的机制,最终引出“鸭子类型”的哲学。学生将学习如何使用代码块和 Proc 定义灵活接口,并利用 Ruby 的异常与强制转换协议实现健壮系统。
学习成果:
- 执行复杂的方法查找并使用 super 和 alias 操纵方法行为。
- 使用类/模块定义和属性声明构建结构化代码库。
- 使用代码块、闭包和 Proc 对象实现函数式编程模式。
概述: 本课深入探讨 Ruby 对象的内部架构,重点关注实例、类与元类之间的复杂关系。学生将掌握通过对象专属类和元编程技术实现动态行为,同时学习使用 Ruby 内置的安全级别和数据污染机制来保障应用安全。
学习成果:
- 映射类与对象之间交互的内部指针结构(klass 与 super)。
- 使用单例类实现对象专属行为,并通过类实例变量管理状态。
- 应用元编程和对象冻结技术,创建健壮、不可变或动态生成的代码。
概述: 本模块探讨 Ruby 强大的内省能力,使开发者能够查看对象与类内部,动态调用方法,并通过序列化(Marshaling)和 YAML 管理对象持久化。同时提供一份完整的 Ruby 标准库参考与应用指南,涵盖核心类、网络协议、数据存储和系统工具。
学习成果:
- 实现反射:使用 ObjectSpace、respond_to? 和 send 在运行时检查和操作对象。
- 掌握序列化:应用 Marshaling 与 YAML 策略持久化对象状态,并支持分布式 Ruby(DRb)通信。
- 使用标准库:导航并使用专用模块,包括网络(Net::HTTP)、XML/Web(REXML、CGI)以及数据存储(PStore、CSV、SDBM)。
课程
Lesson
This lesson introduces the history and philosophy of the Ruby language while guiding students through setting up a stable development environment on POSIX systems. Students will learn to manage Ruby versions, utilize package managers for dependencies, and execute scripts using standard Shebang notation.
This lesson explores inheritance in Ruby, focusing on how subclasses establish an "is-a" relationship with parent classes to evolve state and behavior. Students will learn to use the super keyword to manage initialization chains and method augmentation while maintaining encapsulation to avoid tight coupling.
This lesson explores Ruby’s scalar data types, focusing on the automatic memory management between Fixnum and Bignum integers and the use of strict conversion methods. Students will also learn to optimize string manipulation and data extraction workflows by constructing complex regular expressions.
AI035: Ch. 6-7: Method Logic and Expression Control (Lesson 4) This lesson explores flexible method definitions in Ruby, focusing on the use of default parameters, the splat operator for argument handling, and expression-based return values. Students will learn how to build adaptive interfaces and utilize parallel assignment to improve code efficiency and readability.
This lesson explores Ruby's exception hierarchy, teaching students how to treat errors as data objects to build more resilient and maintainable applications. You will learn to implement effective error handling by distinguishing between recoverable application errors and fatal system failures while applying modularity principles to improve system stability.
This lesson explores Ruby's stream-based I/O architecture, focusing on how the IO class and Kernel methods provide a unified, bidirectional interface for interacting with files, sockets, and standard streams. Students will learn to effectively manage data flow, utilize standard filters, and apply best practices for input handling and output buffering in Ruby applications.
This lesson explores the principles of reliable Ruby programming through systematic unit testing, debugging, and performance optimization. Students will learn to implement the Test::Unit framework to isolate logic errors, utilize assertions for real-time validation, and apply profiling techniques to improve code efficiency.
This lesson explores the Ruby operational environment, focusing on efficient development workflows through command-line utilities, interactive debugging with IRB, and documentation management using RDoc. Students will learn to leverage Ruby's built-in tools for stream processing, project navigation, and graceful program termination.
This lesson explores the RubyGems ecosystem, focusing on how architectural isolation and runtime path management allow multiple versions of the same library to coexist. Students will learn to resolve dependency trees, manage version constraints, and use RubyGems to prevent version collisions in complex development environments.
This lesson explores the transition from linear terminal scripts to event-driven GUI applications by integrating remote SOAP services into Ruby interfaces. Students will learn to use WSDL dynamic discovery and data sanitization techniques to build responsive, user-friendly dashboards that effectively manage complex, nested data.
This lesson explores how to extend Ruby with C to achieve high-performance execution while maintaining compatibility with Ruby’s object model. Students will learn to use the VALUE type as a bridge between languages, register C functions as Ruby methods, and manage memory safely within the Ruby Garbage Collector.
This lesson explores the structural anatomy of Ruby scripts, focusing on the language's execution lifecycle, reserved keywords, and the use of magic constants like `__FILE__`, `__LINE__`, and `DATA`. Students will learn to implement modular design patterns, such as the `if __FILE__ == $0` execution gate, to create versatile scripts that function as both standalone tools and reusable libraries.
This lesson explores the core philosophy of duck typing and the mechanics of dynamic method dispatch in Ruby. Students will learn how to implement polymorphic interfaces, handle variable arguments, and utilize blocks and the yield mechanism to create flexible, message-driven code.
This lesson explores the internal architecture of Ruby objects, focusing on how the `klass` and `super` pointers facilitate method resolution and inheritance. Students will learn how Ruby decouples instance state from class-based logic and how virtual classes are utilized to manage singleton methods.
This lesson explores Ruby’s reflection and introspection capabilities, teaching students how to dynamically inspect object interfaces, hierarchies, and capabilities at runtime. Participants will learn to implement meta-programming patterns like duck typing and dynamic dispatch while understanding the performance trade-offs of these powerful tools.