HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
Ruby Phrasebook
Jason Clinton
Essential Code and Commands
Ruby Phrasebook gives you the code you need to quickly and effectively work with Ruby, one of the fastest-growing languages in the world thanks to popular new Ruby technologies like Ruby on Rails.
Concise and Accessible
Easy to carry and easy to use—lets you ditch all those bulky books for one portable pocket guide
Flexible and Functional
Packed with more than 100 customizable code snippets—so you can readily code functional Ruby in just about any situation
Jason Clinton uses Ruby daily in system administration and development for Advanced Clustering Technologies, a Linux Beowulf cluster integrator. He has been working in the computer industry for more than a decade and is actively involved in the Kansas City Ruby Users Group (KCRUG), serving as administrator of the group’s web site and mailing list.
Register your book at informit.com/register for convenient access to downloads, updates, and corrections as they become available.
Programming / Ruby
Working with Collections in Ruby
Introduction 1
Audience 1
How to Use This Book 2
Conventions 2
Acknowledgments 4
1 Converting Between Types 5
Number from a String 6
Number to Formatted String 7
String to Array and Back Again 10
String to Regular Expression and Back Again 12
Array to Hash and Back Again 13
Array to Set and Back Again 15
Floating-Point, Integer, and Rational Numbers 15
2 Working with Strings 19
Searching Strings 20
Searching Strings with Regular Expressions 21
Replacing Substrings 23
Replacing Substrings using SprintF 24
Replacing Substrings using Regu 26
Working with Unicode 26
Sanitizing Input 27
Working with Line Endings 28
Processing Large Strings 30
Comparing Strings 31
Checksumming a String (MD5 or Otherwise) 31
Encrypting a String 32
3 Working with Collections 35
Slicing an Array 35
Iterating over an Array 37
Creating Enumerable Classes 38
Sorting an Array 40
Iterating over Nested Arrays 41
Modifying All the Values in an Array 42
Sorting Nested Arrays 42
Building a Hash from a Config File 44
Sorting a Hash by Key or Value 45
Eliminating Duplicate Data from Arrays (Sets) 46
Working with Nested Sets 47
4 Working with Objects 49
Inspecting Objects and Classes 50
String Presentation of Objects 50
Ruby-Style Polymorphisms (“Duck Typing”) 51
Comparing Objects 52
Serializing Objects 53
Duplication 54
Protecting an Object Instance 55
Garbage Collecting 56
Using Symbols 57
5 Working with Pipes 61
Determining Interactive Standard Pipes 62
Synchronizing STDERR with STDOUT 63
Capturing the Output of a Child Process 64
Implementing a Progress Bar 65
Creating a Secured Password Prompt 66
6 Working with Files 69
Opening and Closing Files 69
Searching and Seeking Large File Contents 70
When to Use Binary Mode (Win32) 73
Obtaining an Exclusive Lock 74
Copying, Moving, and Deleting Files 74
7 Manipulating Text 77
Parsing an LDIF 77
Parsing a Simple Config File 78
Interpolating One Text File 79
Sorting the Contents of a File 80
Processing a passwd File 81
8 Ruby One-Liners 83
Simple Search 84
Counting Lines in a File 84
Head or Tail of a File 84
MD5 or SHA1 Hash 85
Simple HTTP Fetch 86
Simple TCP Connect 87
Escaping HTML 87
Deleting Empty Directories 88
Adding Users from a Text File 88
Delete All the Files Just Extracted 89
9 Processing XML 91
Opening an XML File 92
Accessing an Element (Node) 93
Getting a List of Attributes 95
Adding an Element 96
Changing an Element’s Enclosed Text 97
Deleting an Element 98
Adding an Attribute 98
Changing an Attribute 99
Deleting an Attribute 99
Escaping Characters for XML 100
Transforming Using XSLT 100
Validating Your XML 102
A Simple RSS Parser 103
10 Rapid Applications Development with GUI
Toolkits 107
A Simple GTK+ Hello World 108
Using Glade 110
A Simple Qt Hello World 115
Attaching a Signal Handler to a Qt
Widget Slot 116
Using Qt Designer 118
Attaching Signal Handlers to Qt Designer
Generated Code 124
11 Simple CGI Forms 127
Processing a Web Form 128
Returning Tabled Results 131
Escaping Input 134
Locking Down Ruby 136
Receiving an Uploaded File 137
Representing Data Graphically 138
12 Connecting to Databases 143
Opening (and Closing) a MySQL Database
Connection 144
Creating a Table 145
Getting a List of Tables 146
Adding Rows to a Table 146
Iterating Over Queried Rows 147
Deleting Rows 147
Deleting a Table 148
13 Working with Networking and Sockets 151
Connecting to a TCP Socket 152
Running a TCP Server on a Socket 153
Serializing Objects with YAML 156
Network Objects with Distributed Ruby 158
Using Net::HTTP 159
Using Webrick 160
14 Working with Threads 163
Creating a Thread 164
Using a Timer 165
Killing a Thread 168
Synchronizing Thread Communication 169
Multithreaded Exception Gathering 172
15 Documenting Your Ruby 175
Documenting Ruby Code 176
Typographic Conventions Used 177
Overriding Method Signatures in
Documentation 179
Hiding a Module, Class, or Method 180
Providing Program Usage Help 180
Generating HTML Documentation 182
Generating and Installing Documentation for ri 183
16 Working with Ruby Packages 185
Installing a Module 187
Removing a Module 188
Searching for a Module 188
Updating Modules 188
Examining a Module 189
Packaging Your Module with Hoe 189
Creating a Simple Test Case 190
Distributing Your Module on RubyForge 191
Making Rakefile Standalone 192
Index 195