Brian Marick — «Everyday Scripting with Ruby

Brian Marick — «Everyday Scripting with Ruby

What readers are saying about Everyday Scripting with Ruby What a wondrous collection of recipes, guidelines, warnings, compre- hensive examples, metaphors, exercises, and questions! It’s a terrific value to software testing practitioners who want to get the most from their test automation effort. Grigori Melnik Lecturer, University of Calgary A fantastic type-along-with-me introduction to a powerful scripting language that starts in the shallows and then moves into the depths turning the reader into an accomplished Ruby scripter, almost with- out them noticing it! Erik Petersen Emprove Finally a hands-on book that is filled with gems of wisdom for the test- ing community. By following the book’s easy-to-read chapters, real-life code samples, and superb coverage of complex topics like test-driven design and inheritance, a tester will not only take her testing career to the next level but also contribute immensely to the software develop- ment at her organization. Gunjan Doshi VP of Product Development and Process Excellence, Community Connect, Inc Marick explains the Ruby language using a series of short, practical examples. Watir users and other testers who want to learn Ruby will find it very accessible. Bret Pettichord Lead Developer, Watir When you’ve read this book, you will be able to automate software tests, which will give you an edge on most of your QA workmates. You will be able to program in Ruby, which is a joy in itself. You will have created several very useful utilities and will know how to adapt them to meet your particular needs. All of the above will have been achieved briskly and pleasantly. You will become a more effective tester and, most likely, will have a fine time in the process. George Hawthorne Consultant, Oblomov Consulting The book is an excellent read, is very informative, and covers a lot of ground in a relatively slim book. I think this is always a good idea. I have a lot of 800+ page tech books that I’ve read about the first half or two thirds of, because they are padded toward the end with very esoteric information. This book held my interest throughout—I have a full-time job and a ten-month-old son and still managed to get through these examples in around a week! Brian’s personality comes through (e.g., the Kennel containing Barkers) in a good way that helps rather than hinders in understanding the material. Paddy Healey Enterprise Systems Engineer, Aventail Corporation The chapters, examples, and exercises on regular expressions are worth the cost of the book alone! Everything else is more than just gravy—it’s every kind of dessert you didn’t know you could have. Whether you are just beginning to script or have been scripting for several years, this book will be an invaluable resource. The examples and exercises, Ruby facts, step-by-step approach, and explanations will help you kick up your automation efforts to a whole new level! Paul Carvalho Consultant, Software Testing and Quality Services Everyday Scripting with Ruby For Teams, Testers, and You Brian Marick The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Many of the designations used by manufacturers and sellers to distinguish their prod- ucts are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragmaticprogrammer.com Copyright © 2006 Brian Marick. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 0-9776166-1-4 ISBN-13: 978-0-9776166-1-9 Printed on acid-free paper with 85% recycled, 30% post-consumer content. First printing, December 2006 Version: 2007-1-25 To Dawn, my Best Beloved, best friend, and role model And to shoemakers’ children everywhere Contents 1 Introduction 13 1.1 How the Book Works ..................... 15 1.2 An Outline of the Book ................... 16 1.3 Service After the Sale .................... 17 1.4 Supplements ......................... 18 1.5 Acknowledgments ...................... 18 2 Getting Started 19 2.1 Download the Practice Files ................. 19 2.2 In the Beginning Was the Command Line ......... 20 2.3 Do You Need to Install Ruby? ................ 22 2.4 Installing Ruby ........................ 22 2.5 Your Two Basic Tools .................... 23 2.6 Prompts, Command Lines, Prompts, and irb . 24 2.7 It’s Time to Make Mistakes ................. 26 I The Basics 29 3 A First Script: Comparing File Inventories 30 3.1 A Script in Action ....................... 30 3.2 The Ruby Universe ...................... 31 3.3 Objects Send and Receive Messages ............ 31 3.4 Variables Name Objects ................... 33 3.5 Comparing Arrays ...................... 34 3.6 Printing to the Screen .................... 35 3.7 Making a Script ........................ 36 3.8 Where Do We Stand? .................... 38 3.9 Exercises ........................... 38 CONTENTS 9 4 Ruby Facts: Arrays 39 5 Three Improvements and a Bug Fix 43 5.1 Command-line Arguments ................. 43 5.2 Ignoring Case ......................... 45 5.3 Methods ............................ 49 5.4 Dissecting Strings ...................... 51 5.5 Fixing a Bug ......................... 53 5.6 Where Do We Stand? .................... 55 5.7 Prelude to the Exercises ................... 56 5.8 Exercises ........................... 58 6 Ruby Facts: If, Equality Testing, and Unless 61 6.1 if ...elsif ...else ....................... 61 6.2 When Are Objects Equal? .................. 63 6.3 A Shorthand Version of if .................. 63 6.4 unless ............................. 64 6.5 The Question Mark Operator ................ 64 II Growing a Script 66 7 The Churn Project: Writing Scripts without Fuss 67 7.1 The Project .......................... 67 7.2 Building a Solution ..................... 69 7.3 Where Do We Stand? .................... 91 7.4 Exercises ........................... 91 8 Ruby Facts: Booleans 94 8.1 Other Boolean Operators .................. 94 8.2 Precedence .......................... 94 8.3 Every Object Is a Truth Value ............... 96 8.4 Boolean Expressions Can Select Objects ......... 96 9 Our Friend, the Regular Expression 98 9.1 Regular Expressions Match Strings ............ 99 9.2 Dissecting Strings with Regular Expressions . 101 9.3 Reordering an Array ..................... 102 9.4 Where Do We Stand? .................... 104 9.5 Exercises ........................... 104 CONTENTS 10 10 Ruby Facts: Regular Expressions 106 10.1 Special Characters ...................... 106 10.2 Grouping and Alternatives ................. 108 10.3 Taking Strings Apart ..................... 108 10.4 Variables Behind the Scenes ................ 109 10.5 Regular Expression Options ................ 109 10.6 Wait, There’s More. .................... 110 10.7 Exercises ........................... 110 11 Classes Bundle Data and Methods 112 11.1 Classes Define Methods ................... 115 11.2 Objects Contain Data .................... 116 11.3 Where Do We Stand? .................... 120 11.4 Exercises ........................... 121 12 Ruby Facts: Classes (with a Side Order of Symbols) 126 12.1 Defining Accessors ...................... 126 12.2 Self ............................... 129 12.3 Class Methods ........................ 133 12.4 Class Variables and Globals ................ 136 12.5 Exercises ........................... 136 III Working in a World Full of People 138 13 Scraping Web Pages with Regular Expressions 139 13.1 Treating Web Pages Like Files ............... 140 13.2 Restricting Attention to Part of the Page ......... 142 13.3 Plucking Out the Title and Authors ............ 144 13.4 Hashes Store Named Data ................. 146 13.5 Taking the Trip ........................ 147 13.6 Exercise Yourself ....................... 149 14 Other Ways of Working with Web Applications 152 14.1 Handling XHTML ....................... 152 14.2 Driving the Browser ..................... 154 14.3 Direct Access to Underlying Protocols ........... 155 CONTENTS 11 15 Working with Comma-Separated Values 158 15.1 The CSV Library ....................... 159 15.2 Using Blocks for Automatic Cleanup ........... 159 15.3 More CSV Operations .................... 160 15.4 Applying It All to affinity-trip.rb .............. 160 15.5 Discovering and Understanding Classes in the Standard Library 161 15.6 Replacing Code with Data .................. 163 16 Ruby Facts: Hashes 166 17 Ruby Facts: Argument Lists 169 17.1 Optional Arguments ..................... 169 17.2 Rest Arguments ....................... 170 17.3 Keyword Arguments ..................... 171 18 Downloading Helper Scripts and Applications 174 18.1 Finding Packages ......................

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    297 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us