Ruby phrasebook 9780672328978, 0672328976, 1071081101

Ruby Phrasebook Jason Clinton Essential Code and CommandsRuby Phrasebookgives you the code you need to quickly and effec

435 112 1MB

English Pages 204 pages: illustrations; 18 cm [219] Year 2008;2009

Report DMCA / Copyright

DOWNLOAD FILE

Ruby phrasebook
 9780672328978, 0672328976, 1071081101

Table of contents :
Cover......Page 1
Contents......Page 5
Audience......Page 13
Conventions......Page 14
Acknowledgments......Page 16
1 Converting Between Types......Page 17
Number from a String......Page 18
Number to Formatted String......Page 19
String to Array and Back Again......Page 22
String to Regular Expression and Back Again......Page 24
Array to Hash and Back Again......Page 25
Floating-Point, Integer, and Rational Numbers......Page 27
2 Working with Strings......Page 31
Searching Strings......Page 32
Searching Strings with Regular Expressions......Page 33
Replacing Substrings......Page 35
Replacing Substrings using SprintF......Page 36
Working with Unicode......Page 38
Sanitizing Input......Page 39
Working with Line Endings......Page 40
Processing Large Strings......Page 42
Checksumming a String (MD5 or Otherwise)......Page 43
Encrypting a String......Page 44
Slicing an Array......Page 47
Iterating over an Array......Page 49
Creating Enumerable Classes......Page 50
Sorting an Array......Page 52
Iterating over Nested Arrays......Page 53
Sorting Nested Arrays......Page 54
Building a Hash from a Config File......Page 56
Sorting a Hash by Key or Value......Page 57
Eliminating Duplicate Data from Arrays (Sets)......Page 58
Working with Nested Sets......Page 59
4 Working with Objects......Page 61
String Presentation of Objects......Page 62
Ruby-Style Polymorphisms (“Duck Typing”)......Page 63
Comparing Objects......Page 64
Serializing Objects......Page 65
Duplication......Page 66
Protecting an Object Instance......Page 67
Garbage Collecting......Page 68
Using Symbols......Page 69
5 Working with Pipes......Page 73
Determining Interactive Standard Pipes......Page 74
Synchronizing STDERR with STDOUT......Page 75
Capturing the Output of a Child Process......Page 76
Implementing a Progress Bar......Page 77
Creating a Secured Password Prompt......Page 78
Opening and Closing Files......Page 81
Searching and Seeking Large File Contents......Page 82
When to Use Binary Mode (Win32)......Page 85
Copying, Moving, and Deleting Files......Page 86
Parsing an LDIF......Page 89
Parsing a Simple Config File......Page 90
Interpolating One Text File......Page 91
Sorting the Contents of a File......Page 92
Processing a passwd File......Page 93
8 Ruby One-Liners......Page 95
Head or Tail of a File......Page 96
MD5 or SHA1 Hash......Page 97
Simple HTTP Fetch......Page 98
Escaping HTML......Page 99
Adding Users from a Text File......Page 100
Delete All the Files Just Extracted......Page 101
9 Processing XML......Page 103
Opening an XML File......Page 104
Accessing an Element (Node)......Page 105
Getting a List of Attributes......Page 107
Adding an Element......Page 108
Changing an Element’s Enclosed Text......Page 109
Adding an Attribute......Page 110
Deleting an Attribute......Page 111
Transforming Using XSLT......Page 112
Validating Your XML......Page 114
A Simple RSS Parser......Page 115
10 Rapid Applications Development with GUI Toolkits......Page 119
A Simple GTK+ Hello World......Page 120
Using Glade......Page 122
A Simple Qt Hello World......Page 127
Attaching a Signal Handler to a Qt Widget Slot......Page 128
Using Qt Designer......Page 130
Attaching Signal Handlers to Qt Designer Generated Code......Page 136
11 Simple CGI Forms......Page 139
Processing a Web Form......Page 140
Returning Tabled Results......Page 143
Escaping Input......Page 146
Locking Down Ruby......Page 148
Receiving an Uploaded File......Page 149
Representing Data Graphically......Page 150
12 Connecting to Databases......Page 155
Opening (and Closing) a MySQL Database Connection......Page 156
Creating a Table......Page 157
Adding Rows to a Table......Page 158
Deleting Rows......Page 159
Deleting a Table......Page 160
13 Working with Networking and Sockets......Page 163
Connecting to a TCP Socket......Page 164
Running a TCP Server on a Socket......Page 165
Serializing Objects with YAML......Page 168
Network Objects with Distributed Ruby......Page 170
Using Net::HTTP......Page 171
Using Webrick......Page 172
14 Working with Threads......Page 175
Creating a Thread......Page 176
Using a Timer......Page 177
Killing a Thread......Page 180
Synchronizing Thread Communication......Page 181
Multithreaded Exception Gathering......Page 184
15 Documenting Your Ruby......Page 187
Documenting Ruby Code......Page 188
Typographic Conventions Used......Page 189
Overriding Method Signatures in Documentation......Page 191
Providing Program Usage Help......Page 192
Generating and Installing Documentation for ri......Page 194
16 Working with Ruby Packages......Page 197
Installing a Module......Page 199
Updating Modules......Page 200
Packaging Your Module with Hoe......Page 201
Creating a Simple Test Case......Page 202
Distributing Your Module on RubyForge......Page 203
Making Rakefile Standalone......Page 204
B-C......Page 207
D......Page 208
F......Page 209
I......Page 210
M......Page 211
P......Page 212
Q-R......Page 213
S......Page 214
T......Page 215
X-Y-Z......Page 216

Polecaj historie