SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
This is the Rough Cut version of the printed book.
For more than a decade, Ruby developers have turned to The Ruby Way for reliable “how-to” guidance on effective Ruby programming. Now, Hal Fulton and André Arko have thoroughly updated this classic guide to cover new language enhancements and developers’ experiences through Ruby 2.1.
The new edition illuminates Ruby 2.1 through 400+ examples, each answering the question: “How do I do this in Ruby?” For each example, they present both a task description and realistic technical constraints. Next, they walk step-by-step through presenting one good solution, offering detailed explanations to promote deeper understanding.
Conveniently organized by topic, The Ruby Way, Third Edition makes it easier than ever to find the specific solution you want—and to write better code by reflecting Ruby’s unique philosophy and spirit.
Coverage includes
All source code for this book may be downloaded at www.rubyhacker.com.
informit.com/aw
informit.com/ruby
rubyhacker.com/therubyway
therubyway.io
Foreword xxiv
Acknowledgments xxviii
About the Authors xxxii
Introduction xxxiii
1 Ruby in Review 1
1.1 An Introduction to Object Orientation 2
1.1.1 What Is an Object? 2
1.1.2 Inheritance 4
1.1.3 Polymorphism 6
1.1.4 A Few More Terms 7
1.2 Basic Ruby Syntax and Semantics 8
1.2.1 Keywords and Identifiers 9
1.2.2 Comments and Embedded Documentation 10
1.2.3 Constants, Variables, and Types 11
1.2.4 Operators and Precedence 13
1.2.5 A Sample Program 14
1.2.6 Looping and Branching 17
1.2.7 Exceptions 22
1.3 OOP in Ruby 25
1.3.1 Objects 26
1.3.2 Built-in Classes 26
1.3.3 Modules and Mixins 28
1.3.4 Creating Classes 29
1.3.5 Methods and Attributes 34
1.4 Dynamic Aspects of Ruby 36
1.4.1 Coding at Runtime 36
1.4.2 Reflection 38
1.4.3 Missing Methods 40
1.4.4 Garbage Collection 40
1.5 Training Your Intuition: Things to Remember 41
1.5.1 Syntax Issues 41
1.5.2 Perspectives in Programming 44
1.5.3 Ruby’s case Statement 47
1.5.4 Rubyisms and Idioms 50
1.5.5 Expression Orientation and Other Miscellaneous Issues 57
1.6 Ruby Jargon and Slang 59
1.7 Conclusion 62
2 Working with Strings 63
2.1 Representing Ordinary Strings 64
2.2 Representing Strings with Alternate Notations 65
2.3 Using Here-Documents 65
2.4 Finding the Length of a String 67
2.5 Processing a Line at a Time 68
2.6 Processing a Character or Byte at a Time 68
2.7 Performing Specialized String Comparisons 69
2.8 Tokenizing a String 71
2.9 Formatting a String 73
2.10 Using Strings as IO Objects 74
2.11 Controlling Uppercase and Lowercase 74
2.12 Accessing and Assigning Substrings 75
2.13 Substituting in Strings 78
2.14 Searching a String