ruby cookbook
908 pág.

ruby cookbook


DisciplinaProgramação I24.772 materiais281.886 seguidores
Pré-visualização50 páginas
585
15.13 Extracting Code into Helper Functions 587
15.14 Refactoring the View into Partial Snippets of Views 588
15.15 Adding DHTML Effects with script.aculo.us 592
15.16 Generating Forms for Manipulating Model Objects 594
15.17 Creating an Ajax Form 598
15.18 Exposing Web Services on Your Web Site 601
15.19 Sending Mail with Rails 604
15.20 Automatically Sending Error Messages to Your Email 606
15.21 Documenting Your Web Site 608
15.22 Unit Testing Your Web Site 609
15.23 Using breakpoint in Your Web Application 613
16. Web Services and Distributed Programming . . . . . . . . . . . . . . . . . . . . . . . . . 616
16.1 Searching for Books on Amazon 617
16.2 Finding Photos on Flickr 620
16.3 Writing an XML-RPC Client 623
16.4 Writing a SOAP Client 625
16.5 Writing a SOAP Server 627
16.6 Searching the Web with Google\u2019s SOAP Service 628
16.7 Using a WSDL File to Make SOAP Calls Easier 630
16.8 Charging a Credit Card 632
16.9 Finding the Cost to Ship Packages via UPS or FedEx 633
16.10 Sharing a Hash Between Any Number of Computers 635
16.11 Implementing a Distributed Queue 639
16.12 Creating a Shared \u201cWhiteboard\u201d 640
16.13 Securing DRb Services with Access Control Lists 644
16.14 Automatically Discovering DRb Services with Rinda 645
16.15 Proxying Objects That Can\u2019t Be Distributed 647
16.16 Storing Data on Distributed RAM with MemCached 650
16.17 Caching Expensive Results with MemCached 652
16.18 A Remote-Controlled Jukebox 655
Table of Contents | xv
17. Testing, Debugging, Optimizing, and Documenting . . . . . . . . . . . . . . . . . . . 661
17.1 Running Code Only in Debug Mode 662
17.2 Raising an Exception 664
17.3 Handling an Exception 666
17.4 Rerunning After an Exception 668
17.5 Adding Logging to Your Application 669
17.6 Creating and Understanding Tracebacks 672
17.7 Writing Unit Tests 674
17.8 Running Unit Tests 677
17.9 Testing Code That Uses External Resources 679
17.10 Using breakpoint to Inspect and Change the State
 of Your Application 684
17.11 Documenting Your Application 686
17.12 Profiling Your Application 691
17.13 Benchmarking Competing Solutions 694
17.14 Running Multiple Analysis Tools at Once 696
17.15 Who\u2019s Calling That Method? A Call Graph Analyzer 697
18. Packaging and Distributing Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
18.1 Finding Libraries by Querying Gem Respositories 702
18.2 Installing and Using a Gem 705
18.3 Requiring a Specific Version of a Gem 708
18.4 Uninstalling a Gem 711
18.5 Reading Documentation for Installed Gems 712
18.6 Packaging Your Code as a Gem 714
18.7 Distributing Your Gems 717
18.8 Installing and Creating Standalone Packages with setup.rb 719
19. Automating Tasks with Rake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
19.1 Automatically Running Unit Tests 725
19.2 Automatically Generating Documentation 727
19.3 Cleaning Up Generated Files 729
19.4 Automatically Building a Gem 731
19.5 Gathering Statistics About Your Code 732
19.6 Publishing Your Documentation 735
19.7 Running Multiple Tasks in Parallel 737
19.8 A Generic Project Rakefile 738
xvi | Table of Contents
20. Multitasking and Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
20.1 Running a Daemon Process on Unix 746
20.2 Creating a Windows Service 749
20.3 Doing Two Things at Once with Threads 752
20.4 Synchronizing Access to an Object 754
20.5 Terminating a Thread 757
20.6 Running a Code Block on Many Objects Simultaneously 760
20.7 Limiting Multithreading with a Thread Pool 763
20.8 Driving an External Process with popen 765
20.9 Capturing the Output and Error Streams
 from a Unix Shell Command 767
20.10 Controlling a Process on Another Machine 768
20.11 Avoiding Deadlock 770
21. User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
21.1 Getting Input One Line at a Time 774
21.2 Getting Input One Character at a Time 776
21.3 Parsing Command-Line Arguments 779
21.4 Testing Whether a Program Is Running Interactively 782
21.5 Setting Up and Tearing Down a Curses Program 782
21.6 Clearing the Screen 785
21.7 Determining Terminal Size 786
21.8 Changing Text Color 788
21.9 Reading a Password 791
21.10 Allowing Input Editing with Readline 792
21.11 Making Your Keyboard Lights Blink 794
21.12 Creating a GUI Application with Tk 796
21.13 Creating a GUI Application with wxRuby 800
21.14 Creating a GUI Application with Ruby/GTK 803
21.15 Creating a Mac OS X Application with RubyCocoa 807
21.16 Using AppleScript to Get User Input 815
22. Extending Ruby with Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817
22.1 Writing a C Extension for Ruby 818
22.2 Using a C Library from Ruby 821
22.3 Calling a C Library Through SWIG 825
22.4 Writing Inline C in Your Ruby Code 827
22.5 Using Java Libraries with JRuby 830
Table of Contents | xvii
23. System Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
23.1 Scripting an External Program 834
23.2 Managing Windows Services 835
23.3 Running Code as Another User 837
23.4 Running Periodic Tasks Without cron or at 839
23.5 Deleting Files That Match a Regular Expression 840
23.6 Renaming Files in Bulk 842
23.7 Finding Duplicate Files 845
23.8 Automating Backups 848
23.9 Normalizing Ownership and Permissions in User Directories 849
23.10 Killing All Processes for a Given User 852
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
xix
Preface1
Life Is Short
This is a book of recipes: solutions to common problems, copy-and-paste code snip-
pets, explanations, examples, and short tutorials.
This book is meant to save you time. Time, as they say, is money, but a span of time
is also a piece of your life. Our lives are better spent creating new things than fight-
ing our own errors, or trying to solve problems that have already been solved. We
present this book in the hope that the time it saves, distributed across all its readers,
will greatly outweigh the time we spent creating it.
The Ruby programming language is itself a wonderful time-saving tool. It makes you
more productive than other programming languages because you spend more time
making the computer do what you want, and less wrestling with the language. But
there are many ways for a Ruby programmer to spend time without accomplishing
anything, and we\u2019ve encountered them all:
\u2022 Time spent writing Ruby implementations of common algorithms.
\u2022 Time spent debugging Ruby implementations of common algorithms.
\u2022 Time spent discovering and working around Ruby-specific pitfalls.
\u2022 Time spent on repetitive tasks (including repetitive programming tasks!) that
could be automated.
\u2022 Time spent duplicating work that someone else has already made publicly available.
\u2022 Time spent searching for a library that does X.
\u2022 Time spent evaluating and deciding between the many libraries that do X.
\u2022 Time spent learning how to use a library because of poor or outdated
documentation.
\u2022 Time lost staying away from a useful technology because it seems intimidating.
xx | Preface
We, and the many contributors to this book, recall vividly our own wasted hours
and days. We\u2019ve distilled our experiences into this book so that you don\u2019t waste your
time\u2014or at least so you enjoyably waste it on more interesting problems.
Our other goal is to expand your interests. If you come to this book wanting to gen-
erate algorithmic music with Ruby then, yes, Recipe 12.14 will save you time over
starting from scratch. It\u2019s more likely that you\u2019d never considered the possibility until
now. Every recipe in this book was developed and written