ruby cookbook
908 pág.

ruby cookbook


DisciplinaProgramação I24.517 materiais279.369 seguidores
Pré-visualização50 páginas
a File 191
6.3 Changing the Permissions on a File 193
6.4 Seeing When a File Was Last Used 196
6.5 Listing a Directory 198
6.6 Reading the Contents of a File 201
6.7 Writing to a File 204
6.8 Writing to a Temporary File 206
6.9 Picking a Random Line from a File 207
6.10 Comparing Two Files 209
6.11 Performing Random Access on \u201cRead-Once\u201d Input Streams 212
x | Table of Contents
6.12 Walking a Directory Tree 214
6.13 Locking a File 217
6.14 Backing Up to Versioned Filenames 220
6.15 Pretending a String Is a File 222
6.16 Redirecting Standard Input or Output 225
6.17 Processing a Binary File 227
6.18 Deleting a File 231
6.19 Truncating a File 232
6.20 Finding the Files You Want 233
6.21 Finding and Changing the Current Working Directory 235
7. Code Blocks and Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
7.1 Creating and Invoking a Block 240
7.2 Writing a Method That Accepts a Block 241
7.3 Binding a Block Argument to a Variable 244
7.4 Blocks as Closures: Using Outside Variables Within a Code Block 246
7.5 Writing an Iterator Over a Data Structure 247
7.6 Changing the Way an Object Iterates 250
7.7 Writing Block Methods That Classify or Collect 253
7.8 Stopping an Iteration 254
7.9 Looping Through Multiple Iterables in Parallel 256
7.10 Hiding Setup and Cleanup in a Block Method 260
7.11 Coupling Systems Loosely with Callbacks 262
8. Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
8.1 Managing Instance Data 269
8.2 Managing Class Data 272
8.3 Checking Class or Module Membership 275
8.4 Writing an Inherited Class 277
8.5 Overloading Methods 279
8.6 Validating and Modifying Attribute Values 281
8.7 Defining a Virtual Attribute 283
8.8 Delegating Method Calls to Another Object 284
8.9 Converting and Coercing Objects to Different Types 287
8.10 Getting a Human-Readable Printout of Any Object 291
8.11 Accepting or Passing a Variable Number of Arguments 293
8.12 Simulating Keyword Arguments 295
8.13 Calling a Superclass\u2019s Method 297
8.14 Creating an Abstract Method 299
Table of Contents | xi
8.15 Freezing an Object to Prevent Changes 302
8.16 Making a Copy of an Object 304
8.17 Declaring Constants 307
8.18 Implementing Class and Singleton Methods 309
8.19 Controlling Access by Making Methods Private 311
9. Modules and Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
9.1 Simulating Multiple Inheritance with Mixins 315
9.2 Extending Specific Objects with Modules 319
9.3 Mixing in Class Methods 321
9.4 Implementing Enumerable: Write One Method, Get 22 Free 322
9.5 Avoiding Naming Collisions with Namespaces 324
9.6 Automatically Loading Libraries as Needed 326
9.7 Including Namespaces 328
9.8 Initializing Instance Variables Defined by a Module 329
9.9 Automatically Initializing Mixed-In Modules 330
10. Reflection and Metaprogramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
10.1 Finding an Object\u2019s Class and Superclass 334
10.2 Listing an Object\u2019s Methods 335
10.3 Listing Methods Unique to an Object 337
10.4 Getting a Reference to a Method 339
10.5 Fixing Bugs in Someone Else\u2019s Class 341
10.6 Listening for Changes to a Class 343
10.7 Checking Whether an Object Has Necessary Attributes 345
10.8 Responding to Calls to Undefined Methods 347
10.9 Automatically Initializing Instance Variables 351
10.10 Avoiding Boilerplate Code with Metaprogramming 352
10.11 Metaprogramming with String Evaluations 355
10.12 Evaluating Code in an Earlier Context 357
10.13 Undefining a Method 358
10.14 Aliasing Methods 361
10.15 Doing Aspect-Oriented Programming 364
10.16 Enforcing Software Contracts 367
11. XML and HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
11.1 Checking XML Well-Formedness 372
11.2 Extracting Data from a Document\u2019s Tree Structure 374
11.3 Extracting Data While Parsing a Document 376
xii | Table of Contents
11.4 Navigating a Document with XPath 377
11.5 Parsing Invalid Markup 380
11.6 Converting an XML Document into a Hash 382
11.7 Validating an XML Document 385
11.8 Substituting XML Entities 388
11.9 Creating and Modifying XML Documents 390
11.10 Compressing Whitespace in an XML Document 394
11.11 Guessing a Document\u2019s Encoding 395
11.12 Converting from One Encoding to Another 396
11.13 Extracting All the URLs from an HTML Document 398
11.14 Transforming Plain Text to HTML 401
11.15 Converting HTML Documents from the Web into Text 402
11.16 A Simple Feed Aggregator 405
12. Graphics and Other File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
12.1 Thumbnailing Images 409
12.2 Adding Text to an Image 412
12.3 Converting One Image Format to Another 415
12.4 Graphing Data 417
12.5 Adding Graphical Context with Sparklines 421
12.6 Strongly Encrypting Data 424
12.7 Parsing Comma-Separated Data 426
12.8 Parsing Not-Quite-Comma-Separated Data 429
12.9 Generating and Parsing Excel Spreadsheets 431
12.10 Compressing and Archiving Files with Gzip and Tar 433
12.11 Reading and Writing ZIP Files 436
12.12 Reading and Writing Configuration Files 437
12.13 Generating PDF Files 439
12.14 Representing Data as MIDI Music 443
13. Databases and Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
13.1 Serializing Data with YAML 450
13.2 Serializing Data with Marshal 454
13.3 Persisting Objects with Madeleine 455
13.4 Indexing Unstructured Text with SimpleSearch 458
13.5 Indexing Structured Text with Ferret 459
13.6 Using Berkeley DB Databases 463
13.7 Controlling MySQL on Unix 465
13.8 Finding the Number of Rows Returned by a Query 466
Table of Contents | xiii
13.9 Talking Directly to a MySQL Database 468
13.10 Talking Directly to a PostgreSQL Database 470
13.11 Using Object Relational Mapping with ActiveRecord 473
13.12 Using Object Relational Mapping with Og 477
13.13 Building Queries Programmatically 481
13.14 Validating Data with ActiveRecord 485
13.15 Preventing SQL Injection Attacks 487
13.16 Using Transactions in ActiveRecord 490
13.17 Adding Hooks to Table Events 492
13.18 Adding Taggability with a Database Mixin 495
14. Internet Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
14.1 Grabbing the Contents of a Web Page 500
14.2 Making an HTTPS Web Request 502
14.3 Customizing HTTP Request Headers 504
14.4 Performing DNS Queries 506
14.5 Sending Mail 508
14.6 Reading Mail with IMAP 512
14.7 Reading Mail with POP3 516
14.8 Being an FTP Client 520
14.9 Being a Telnet Client 522
14.10 Being an SSH Client 525
14.11 Copying a File to Another Machine 527
14.12 Being a BitTorrent Client 529
14.13 Pinging a Machine 531
14.14 Writing an Internet Server 532
14.15 Parsing URLs 534
14.16 Writing a CGI Script 537
14.17 Setting Cookies and Other HTTP Response Headers 540
14.18 Handling File Uploads via CGI 543
14.19 Running Servlets with WEBrick 546
14.20 A Real-World HTTP Client 551
15. Web Development: Ruby on Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
15.1 Writing a Simple Rails Application to Show System Status 557
15.2 Passing Data from the Controller to the View 560
15.3 Creating a Layout for Your Header and Footer 563
15.4 Redirecting to a Different Location 565
15.5 Displaying Templates with Render 567
xiv | Table of Contents
15.6 Integrating a Database with Your Rails Application 570
15.7 Understanding Pluralization Rules 573
15.8 Creating a Login System 575
15.9 Storing Hashed User Passwords in the Database 579
15.10 Escaping HTML and JavaScript for Display 581
15.11 Setting and Retrieving Session Information 582
15.12 Setting and Retrieving Cookies