具体描述
Ruby is a free and powerful programming language that can be used to develop programs to meet nearly any programming challenge, including scripting, application programming and Web development. This new text teaches Ruby programming through a lively hands-on approach and a focus on game development. Students begin by learning the fundamentals of computer programming and will move on to mastering the concepts and principles involved in Ruby programming. Topics covered include formulating regular expressions, basic debugging techniques, and performing file and folder administration. For those students interested in learning how to do Web development with Ruby using Ruby on Rails, this book also provides the necessary prerequisite foundation and introduction.
作者简介
Jerry Lee Ford, Jr. is an author, educator, and an IT professional with over 18 years of experience in information technology, including roles as an automation analyst, technical manager, technical support analyst, automation engineer, and security analyst. He is the author of 23 other books and co-author of two additional books. His published works include AppleScript Studio Programming for the Absolute Beginner, Microsoft Windows PowerShell Programming for the Absolute Beginner, Visual Basic 2005 Express Programming for the Absolute Beginner, VBScript Professional Projects, Microsoft Windows Shell Scripting and WSH Administrator's Guide, Microsoft Windows Shell Scripting for the Absolute Beginner, Learn JavaScript in a Weekend, and Microsoft Windows XP Professional Administrator's Guide.
Ford has a master's degree in business administration from Virginia Commonwealth University in Richmond, Virginia and has over five years of experience as an adjunct instructor teaching networking courses in information technology.
目录信息
ch. 1 Ruby Basics ____ p.1
Project Preview: The Ruby Joke Game ____ p.2
A Brief History of Computers and Computer Programming ____ p.5
The Mechanized Era ____ p.5
The First Generation ____ p.6
The Second Generation ____ p.6
The Third Generation ____ p.7
The Fourth Generation ____ p.8
The Fifth Generation ____ p.9
1990 and Beyond ____ p.9
Introducing Ruby ____ p.10
Ruby is Simple Yet Powerful ____ p.11
Ruby is Object Oriented ____ p.13
Ruby is Extremely Flexible ____ p.14
Ruby Exists in Many Different Environments ____ p.14
Getting Ready to Work with Ruby ____ p.15
Determining Whether Ruby is Already Installed ____ p.15
Installing or Upgrading Ruby ____ p.17
Working with Ruby ____ p.21
Working at the Command Prompt ____ p.21
IRB---Interactive Ruby ____ p.21
fxri-Interactive Ruby Help and Console ____ p.23
Developing Ruby Programs ____ p.24
Creating Ruby Programs on Microsoft Windows ____ p.24
Creating Ruby Programs on Mac OS X ____ p.24
Creating Ruby Programs on Linux and UNIX ____ p.24
Using a Cross-Platform Ruby Editor ____ p.24
Creating Your First Ruby Program ____ p.25
Running Your Ruby Program ____ p.26
Back to the Ruby Joke Game ____ p.27
Designing the Game ____ p.27
Running Your New Ruby Program Game ____ p.32
Summing Up ____ p.35
Comprehension Check ____ p.36
Reinforcement Exercises ____ p.38
Discovery Projects ____ p.39
ch. 2 Interacting with Ruby ____ p.41
Project Preview: The Ruby Tall Tale Game ____ p.42
Getting to Know IRB ____ p.46
Starting an IRB Session ____ p.46
Working with Multiple IRB Sessions ____ p.48
Accessing IRB Online ____ p.48
Working with IRB ____ p.49
Executing Ruby Statements ____ p.49
Following Ruby's Syntax Rules ____ p.50
Executing Incomplete Statements ____ p.51
Using IRB to Test Ruby Scripts ____ p.51
Using IRB to Perform Mathematical Calculations ____ p.53
Using IRB as a Calculator ____ p.53
Accessing Methods Stored in the Math Module ____ p.54
Operator Precedence ____ p.54
Overriding Operator Precedence ____ p.55
Integers Versus Floating-Point Numbers ____ p.56
Accessing Ruby Documentation ____ p.56
Back to the Ruby Tall Tale Game ____ p.59
Designing the Ruby Tall Tale Game ____ p.60
Running Your New Ruby Script Game ____ p.65
Summing Up ____ p.70
Comprehension Check ____ p.71
Reinforcement Exercises ____ p.74
Discovery Projects ____ p.75
ch. 3 Working with Strings, Objects, and Variables ____ p.77
Project Preview: The Ruby Virtual Crazy 8-Ball Game ____ p.78
Creating and Formatting Text Strings ____ p.81
Modifying Text String Format ____ p.82
String Interpolation ____ p.83
Manipulating String Contents ____ p.83
Concatenating Text Strings ____ p.84
Multiplying Text Strings ____ p.84
Comparing Text Strings ____ p.85
Creating Multiline Text Strings ____ p.85
Working with String Class Methods ____ p.87
Object-Oriented Programming ____ p.88
Defining a New Class ____ p.88
Defining Class Properties ____ p.89
Instantiating and Interacting with New Objects ____ p.89
Defining Class Methods ____ p.90
Inheritance ____ p.91
Converting from One Class to Another ____ p.92
Implicit Class Conversion ____ p.93
Explicit Class Conversion ____ p.93
Storing and Retrieving Data ____ p.95
Naming Variables ____ p.96
Variable Assignments ____ p.96
Variable Scope ____ p.97
Storing Data That Does Not Change ____ p.98
Back to the Ruby Virtual Crazy 8-Ball Game ____ p.100
Designing the Game ____ p.100
Running Your New Ruby Script Game ____ p.107
Summing Up ____ p.107
Comprehension Check ____ p.108
Reinforcement Exercises ____ p.111
Discovery Projects ____ p.112
ch. 4 Implementing Conditional Logic ____ p.114
Project Preview: The Ruby Typing Challenge Game ____ p.115
Using Conditional Logic to Create Adaptive Scripts ____ p.119
Performing Alternative Types of Comparisons ____ p.121
Conditional Logic Modifiers ____ p.122
The if Modifier ____ p.122
The unless Modifier ____ p.122
Working with if and unless Expressions ____ p.123
Creating if Expressions ____ p.123
Replacing if Modifiers with if Expressions ____ p.124
Creating unless Expressions ____ p.126
Using case Blocks to Analyze Data ____ p.127
Using the Ternary Operator ____ p.129
Nesting Conditional Statements ____ p.129
Combining and Negating Logical Comparison Operations ____ p.130
Back to the Ruby Typing Challenge Game ____ p.132
Designing the Game ____ p.132
Running Your New Ruby Script Game ____ p.140
Summing Up ____ p.140
Comprehension Check ____ p.141
Reinforcement Exercises ____ p.144
Discovery Projects ____ p.145
ch. 5 Working with Loops ____ p.146
Project Preview: The Superman Movie Trivia Quiz ____ p.147
Understanding Loops ____ p.150
Using Language Constructs to Create Loops ____ p.151
Working with while Loops ____ p.151
Working with until Loops ____ p.152
Working with for...in Loops ____ p.153
Using Loop Modifiers ____ p.154
The while Modifier ____ p.154
The until Modifier ____ p.155
Executing Looping Methods ____ p.156
Working with the each Method ____ p.156
Working with the times Method ____ p.157
Working with the upto Method ____ p.158
Working with the downto Method ____ p.159
Working with the step Method ____ p.160
Working with the loop Method ____ p.161
Altering Loop Execution ____ p.162
Prematurely Terminating Loop Execution ____ p.162
Repeating the Current Execution of a Loop ____ p.163
Skipping to the Next Iteration of a Loop ____ p.163
Restarting a Loop from the Beginning ____ p.164
Back to the Superman Movie Trivia Quiz ____ p.165
Designing the Game ____ p.165
Running Your New Ruby Script Game ____ p.174
Summing Up ____ p.174
Comprehension Check ____ p.175
Reinforcement Exercises ____ p.178
Discovery Projects ____ p.179
ch. 6 Working with Collections of Data ____ p.181
Project Preview: The Ruby Number Guessing Game ____ p.182
Storing and Manipulating Lists Using Arrays ____ p.186
Managing Data Using Arrays ____ p.186
Replacing and Adding Array Items ____ p.188
Determining if an Array is Empty ____ p.190
Retrieving Items from an Array ____ p.190
Using a Loop to Iterate Through the Contents of an Array ____ p.192
Deleting Items from an Array ____ p.192
Sorting the Contents of an Array Alphabetically ____ p.194
Sorting Array Contents in Reverse Alphabetical Order ____ p.194
Searching an Array ____ p.195
Storing and Manipulating Lists Using Hashes ____ p.196
Creating a Hash ____ p.196
Adding a Hash's Key-Value Pairs ____ p.198
Deleting a Hash's Key-Value Pairs ____ p.199
Determining the Number of Key-Value Pairs in a Hash ____ p.200
Retrieving Data Stored in Hashes ____ p.201
Sorting Hash keys ____ p.202
Back to the Ruby Number Guessing Game ____ p.204
Designing the Game ____ p.204
Running Your New Ruby Script Game ____ p.212
Summing Up ____ p.212
Comprehension Check ____ p.214
Reinforcement Exercises ____ p.217
Discovery Projects ____ p.219
ch. 7 Working with Regular Expressions ____ p.221
Project Preview: The Ruby Word Guessing Game ____ p.222
Using Regular Expressions for Data Validation ____ p.227
Matching Basic Patterns ____ p.228
Matching Specific Patterns ____ p.228
Matching Multiple Patterns ____ p.228
Working with Metacharacters ____ p.230
Escaping Metacharacters ____ p.231
Matching Individual Characters ____ p.231
Matching a Pattern at the Beginning of a String ____ p.231
Matching a Pattern at the End of a String ____ p.232
Matching Once or Not at All ____ p.232
Matching Zero or More Times ____ p.232
Matching Any of a Collection of Characters ____ p.233
Other Uses for Regular Expressions ____ p.233
Overcoming Differences in Case ____ p.234
String Substitution ____ p.234
Back to the Ruby Word Guessing Game ____ p.236
Designing the Game ____ p.236
Running Your New Ruby Script Game ____ p.249
Summing Up ____ p.249
Comprehension Check ____ p.251
Reinforcement Exercises ____ p.254
Discovery Projects ____ p.255
ch. 8 Object-Oriented Programming ____ p.257
Project Preview: The Rock, Paper, Scissors Game ____ p.258
Key Features of Object-Oriented Programming ____ p.262
Abstraction ____ p.262
Encapsulation ____ p.263
Inheritance ____ p.266
Polymorphism ____ p.267
Initializing Objects Upon Instantiation ____ p.269
Understanding Variable Scope ____ p.271
Working with Local Variables ____ p.271
Working with Global Variables ____ p.271
Working with Instance Variables ____ p.273
Working with Class Variables ____ p.273
Taking Advantage of Ruby's Built-in Classes ____ p.274
Modifying Ruby Classes ____ p.275
Back to the Rock, Paper, Scissors Game ____ p.277
Designing the Game ____ p.277
Running Your New Ruby Program Game ____ p.286
Summing Up ____ p.286
Comprehension Check ____ p.287
Reinforcement Exercises ____ p.290
Discovery Projects ____ p.292
ch. 9 File and Folder Administration ____ p.294
Project Preview: The Ruby Blackjack Game ____ p.295
Redirecting File Input and Output ____ p.299
Administering Text Files and Folders ____ p.300
Verifying Whether a File or Folder Exists ____ p.300
Retrieving Information About a File or Folder ____ p.301
Creating New Folders ____ p.303
Deleting Files and Folders ____ p.304
Renaming Files ____ p.304
Working with Files and Folders on Different Operating Systems ____ p.305
Reading From and Writing to Text Files ____ p.306
Writing Data to Text Files ____ p.307
Appending Data to the End of Text Files ____ p.308
Reading Data from Text Files ____ p.309
Back to the Ruby Blackjack Game ____ p.311
Designing the Game ____ p.311
Running Your New Ruby Script Game ____ p.321
Summing Up ____ p.322
Comprehension Check ____ p.323
Reinforcement Exercises ____ p.326
Discovery Projects ____ p.329
ch. 10 Debugging ____ p.331
Project Preview: The Ruby Tic-Tac-Toe Game ____ p.332
Types of Script Errors ____ p.335
Syntax Errors ____ p.336
Runtime Errors ____ p.336
Logical Errors ____ p.337
Handling Exceptions ____ p.337
Creating Exception Handlers ____ p.337
Accessing Error Information ____ p.338
Handling Different Types of Errors ____ p.339
Retrying Failed Statements ____ p.339
Tracking a Script's Logical Flow ____ p.341
Finding Bugs Using the Ruby Debugger ____ p.342
Starting the Debugger ____ p.342
Back to the Ruby Tic-Tac-Toe Game ____ p.347
Designing the Game ____ p.347
Running Your New Ruby Script Game ____ p.359
Summing Up ____ p.360
Comprehension Check ____ p.361
Reinforcement Exercises ____ p.364
Discovery Projects ____ p.366
ch. 11 Ruby on Rails Web Development ____ p.368
Project Preview: The Blog Application ____ p.369
Overview of Ruby on Rails ____ p.369
Skills Needed to Work with Ruby on Rails ____ p.371
Required Software ____ p.371
Development Philosophy ____ p.371
Architecture ____ p.372
Working with Database Management Systems ____ p.374
Getting Ruby on Rails Up and Running ____ p.375
Installing Ruby On Rails ____ p.375
Selecting a Web Server ____ p.377
Installing the SQLite Database Management System ____ p.377
Verifying Ruby on Rails Setup ____ p.377
Creating a Demo Application ____ p.378
Verifying the Execution of Your Web Server ____ p.379
Ruby on Rails Applications Files ____ p.381
Back to the Blog Application ____ p.384
Design the Blog Application ____ p.384
Running Your New Ruby on Rails Project ____ p.386
Learning More About Ruby on Rails ____ p.387
Summing Up ____ p.388
Comprehension Check ____ p.390
Reinforcement Exercises ____ p.393
Discovery Projects ____ p.400
Glossary ____ p.401
Index ____ p.407
· · · · · · (收起)
读后感
用户评价
我不得不说,这本书在介绍Ruby的测试驱动开发(TDD)方面,做得非常出色。我之前对TDD一直存在一些误解,觉得它过于耗时,而且增加了开发的复杂度。但这本书通过生动的例子,展示了如何在Ruby中使用`RSpec`等测试框架来编写高质量的测试用例,以及TDD如何帮助我们在开发初期就发现并解决潜在的bug,从而提高代码的可维护性和可靠性。它不仅仅是教我们如何写测试,更重要的是,它传递了一种“先测试后编码”的开发理念,让我深刻体会到TDD在提升开发效率和代码质量方面的重要作用。
这本书的写作风格,用一个词来形容的话,就是“娓娓道来”。它没有那种为了炫技而堆砌晦涩术语的嫌疑,而是用一种非常平和且富有逻辑性的方式,引领读者一步步深入Ruby的世界。我尤其喜欢它在讲解一些稍微复杂一点的概念时,会先从一个更宏观的视角切入,然后再逐步细化,通过大量的实例来佐证。比如,在讲到Ruby的垃圾回收机制时,它没有直接深入到内存管理的底层细节,而是先解释了为什么需要垃圾回收,以及它在Ruby中的大致工作原理,然后再逐步引入更深入的讨论。这种由浅入深的学习路径,对于我这样一个既想理解原理又想掌握实践的读者来说,是非常友好的。
读这本书的过程,就像在拆解一个精巧的机械装置。作者的讲解非常注重细节,对于Ruby语法中的一些细微之处,比如各种符号的含义、代码块(Blocks)的用法、Proc和Lambda的区别等,都做了非常详尽的阐释。我特别欣赏它在讲解代码块时,不仅仅是简单地介绍语法,而是深入分析了代码块在Ruby生态中的重要性,以及它与迭代器(Iterators)配合使用的强大威力。书中关于`yield`关键字的讲解也让我豁然开朗,理解了它是如何将控制权交给代码块的。这种对细节的关注,让我感觉这本书是经过了反复推敲和打磨的,它帮助我建立起对Ruby语言更加牢固和全面的理解。
总的来说,这本书给我带来的不仅仅是知识上的提升,更多的是一种思维方式的改变。它让我看到了Ruby语言背后所蕴含的哲学思想,以及它如何通过简洁而强大的语法,来鼓励开发者写出更具创造性和表现力的代码。书中很多关于代码组织、设计模式以及如何编写“Ruby Way”的代码的建议,都让我受益匪浅。它让我明白,编程不仅仅是写出能运行的代码,更是一种艺术,而Ruby,无疑是这门艺术中一门非常优美的语言。这本书,它真的让我对Ruby编程充满了热爱。
这本书在介绍Ruby的面向对象特性时,我觉得做得相当到位。它不仅仅是停留在“类”和“对象”这些基本概念上,而是深入探讨了Ruby中独特的“Mixins”和“模块”机制。它解释了Mixins如何打破了传统继承的局限性,允许我们以一种更灵活的方式组合代码,实现代码的复用和功能的扩展。书中关于模块(Module)的讲解也很有深度,不仅说明了它作为命名空间的作用,还详细阐述了它如何在Ruby中扮演“混入”的角色,为类和对象提供额外的行为。我印象特别深的是,作者通过一些实际的场景,比如为不同的类添加日志记录功能,来演示如何优雅地使用Mixins,这让我一下子就明白了它的实用价值。
《Ruby Programming》这本书,怎么说呢,我拿到它的时候,心里还是挺期待的。毕竟Ruby语言在Web开发领域的名声在外,而且听说它以优雅和简洁著称。我之前零零散散地接触过一些编程语言,但总觉得它们写起来有时候像是在解一道数学题,枯燥且缺乏灵活性。Ruby编程这本书,我在书店里翻了翻,它的排版和印刷质量都算不错,那种纸张的触感也挺舒服的,不像有些技术书籍那样硬邦邦的,让人读起来就觉得头大。封面设计也比较简洁,没有那种过于花哨或者晦涩难懂的图案,这一点让我感觉作者在内容上应该也是走务实路线的,而不是玩弄一些虚头巴脑的概念。
这本书的章节安排,我觉得非常合理。它从最基础的Ruby语法讲起,然后逐步过渡到更高级的主题,比如并发编程、数据库操作,甚至还触及了一些Ruby on Rails框架的开发理念。这种循序渐进的学习曲线,使得不同阶段的读者都能从中受益。我尤其欣赏它在介绍一些高级主题时,会适当地回顾前面讲过的基础知识,将它们串联起来,形成一个有机的整体。这让我感觉这本书不是孤立地介绍各个知识点,而是构建了一个完整的Ruby知识体系,帮助我更系统地掌握这门语言。
这本书在讲到Ruby的错误处理和异常机制时,我觉得它给了我很大的启发。以往我写代码,对于错误的防范总是做得不够到位,经常会出现一些意料之外的问题。但这本书通过详细介绍Ruby的`begin...rescue...end`结构,以及各种预定义的异常类,让我明白了如何更有效地捕获和处理程序运行时出现的错误。它不仅仅是教会了我们如何写`rescue`块,更重要的是,它强调了如何去设计更健壮的代码,如何通过合理的异常处理来提高程序的稳定性和用户体验。书中还提到了一些关于错误日志记录的最佳实践,这对我日后开发项目非常有指导意义。
老实说,当我翻到书中关于“元编程”的部分时,我有点小小的震撼。之前我对元编程这个概念只停留在一些模糊的理解,觉得它很高深莫测,是只有大神级别的人物才能玩转的东西。但这本书的讲解,却以一种非常清晰且有条理的方式,层层递进地揭示了元编程的奥秘。它通过大量的代码示例,一步步地展示了如何利用Ruby的灵活性来实现元编程,比如通过`define_method`动态地创建方法,或者利用`method_missing`来拦截和处理不存在的方法调用。这些技巧的应用,让我看到了Ruby语言强大的扩展性和生命力,也让我对如何写出更简洁、更具表现力的Ruby代码有了更深的认识。
这本书给我的第一印象,它就像一个经验丰富的引路人,不是那种强硬地把你推着往前走,而是用一种循序渐进、循循善诱的方式,让你在不知不觉中掌握Ruby的精髓。它没有一开始就抛出大量的代码片段,而是先从Ruby的哲学和设计理念入手,让你明白为什么Ruby会是现在这个样子,以及它与其他语言相比的独特之处。我特别喜欢它在解释一些基础概念时,会穿插一些生活化的比喻,比如把对象比作现实生活中的事物,把方法比作对象的行为,这种方式让原本抽象的概念变得生动易懂,大大降低了初学者的入门门槛。而且,这本书的语言风格也很活泼,不像一些技术文档那样死板,读起来不会让人感到枯燥乏味。