Page
1
(This page has no text content)
Page
2
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
Page
3
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
Page
4
(This page has no text content)
Page
5
Everyday Scripting with Ruby For Teams, Testers, and You Brian Marick The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas
Page
6
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
Page
7
To Dawn, my Best Beloved, best friend, and role model And to shoemakers’ children everywhere
Page
8
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
Page
9
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
Page
10
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
Page
11
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 Library161 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 . . . . . . . . . . . . . . . . . . . . . . . 174 18.2 Using setup.rb . . . . . . . . . . . . . . . . . . . . . . . . 175 18.3 Using RubyGems . . . . . . . . . . . . . . . . . . . . . . . 176 18.4 Understanding What You’ve Downloaded . . . . . . . . . 178 19 A Polished Script 180 19.1 The Load Path . . . . . . . . . . . . . . . . . . . . . . . . 181 19.2 Avoiding Filename Clashes . . . . . . . . . . . . . . . . . 181 19.3 Avoiding Class Name Clashes Using Modules . . . . . . 182 19.4 A Script to Do the Work for You . . . . . . . . . . . . . . 184 19.5 Working Without Stepping on Yourself . . . . . . . . . . 187 19.6 The rakefile . . . . . . . . . . . . . . . . . . . . . . . . . . 188 19.7 Location-independent Tests . . . . . . . . . . . . . . . . . 191 19.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 20 Ruby Facts: Modules 195 20.1 Nested Modules . . . . . . . . . . . . . . . . . . . . . . . . 196 20.2 Including Modules . . . . . . . . . . . . . . . . . . . . . . 197 20.3 Classes Are Modules . . . . . . . . . . . . . . . . . . . . . 199
Page
12
CONTENTS 12 21 When Scripts Run into Problems 201 21.1 Use Exceptions to Report Problems . . . . . . . . . . . . 202 21.2 An Error-handling Strategy . . . . . . . . . . . . . . . . . 202 21.3 Your Exception-handling Options . . . . . . . . . . . . . 204 21.4 Methods That Use Blocks . . . . . . . . . . . . . . . . . . 208 21.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 IV The Accomplished Scripter 212 22 Frameworks: Scripting by Filling in Blanks 213 22.1 Using the watchdog Script . . . . . . . . . . . . . . . . . 214 22.2 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . 217 22.3 Gathering User Choices . . . . . . . . . . . . . . . . . . . 223 23 Discovery Is Safer Than Creation 230 23.1 The Story of Barker . . . . . . . . . . . . . . . . . . . . . 231 23.2 What Happens Where? . . . . . . . . . . . . . . . . . . . 234 23.3 Modules Instead of Superclasses . . . . . . . . . . . . . . 239 24 Final Thoughts 241 V The Back of the Book 243 A Glossary 244 B Solutions to Exercises 256 B.1 Solutions for Chapter 3 . . . . . . . . . . . . . . . . . . . 256 B.2 Solutions for Chapter 5 . . . . . . . . . . . . . . . . . . . 258 B.3 Solutions for Chapter 7 . . . . . . . . . . . . . . . . . . . 261 B.4 Solutions for Chapter 9 . . . . . . . . . . . . . . . . . . . 265 B.5 Solutions for Chapter 10 . . . . . . . . . . . . . . . . . . 269 B.6 Solutions for Chapter 11 . . . . . . . . . . . . . . . . . . 270 B.7 Solutions for Chapter 12 . . . . . . . . . . . . . . . . . . 281 B.8 Solutions for Chapter 21 . . . . . . . . . . . . . . . . . . 286 C Bibliography 288 Index 289
Page
13
Chapter 1 Introduction The shoemaker’s children are running around barefoot. People on the outside of software development projects see them spew out a multitude of tools that shift work from people to computers. But the view inside a project is—all too often—different. There, we see days filled with repetitive manual chores. At one desk, a tester is entering test data into a database by hand. At another, a programmer is sifting through the output from a version control system, trying to find the file she wants. At a third, a business analyst is copying data from a report into a spreadsheet. Why are these people doing work that computers could do perfectly well? It’s a matter of knowledge and skill. The tester thinks program- ming is too hard, so he never learned. The programmer knows pro- gramming, but none of her languages makes automating this kind of job easy, and she doesn’t have time to do it the hard way. The analyst once wrote a script to do a similar chore, but it broke when she tried to adapt it to this report. Getting it working would take more time than copying the data by hand, even if she has to copy it six times over the next month. Joe Asks. . . Scripting? Programming? What’s the difference? There isn’t one. I’m using “scripting” for this book because it sounds less imposing and more suited to everyday chores.
Page
14
CHAPTER 1. INTRODUCTION 14 This book is for all those people. • For the person who thinks programming is too hard (our tester): it’s not as hard as all that. Programming has a bad reputation because computers used to be too slow. To make programs run fast enough, programmers had to use programming languages that made them tell the computer all kinds of fiddly details. Com- puters are now fast enough that we can use languages that make them figure out the fiddly little details. As a result, programming is now much easier. • For the person who gets bogged down when writing or changing larger scripts (our analyst): you don’t yet have the skills to master complexity. This book teaches them. It’s a tutorial in the modern style of programming, one that emphasizes writing tests first (test- driven programming), borrowing other people’s work in bits and pieces, growing programs gradually, and constantly keeping them clean. Many scripts will be one-shot: write it, use it, throw it away. But for scripts you plan to keep around, these skills will let you do it. (In truth, many professional programmers I meet haven’t yet learned these particular skills, so they will find this book a useful introduction.) • For the person who knows the wrong languages well (our pro- grammer): languages like Java, C#, C++, and C are perfectly fine languages—in their niche. But their niche is not writing smaller programs quickly, especially not smaller programs that manipu- late text and files rather than numbers and internal data struc- tures. You need to add another language to your repertoire. In this book, you’ll learn a language—Ruby—that is well suited to each of these three audiences. It’s easy to learn and quick to write. While it has the features needed for simple scripts that transform or search text, it also has all the features needed to cope with complexity. If you’re a tester, you’ll be pleased to know that testing is considered one of Ruby’s niches (largely due to Watir, http://wtr.rubyforge.org/, a tool for driving web browsers). If you’re a programmer, you may already know that Ruby has recently become explosively popular because of its “killer app,” Rails (a framework for building web applications, http://www.rubyonrails.org/). Despite that, it’s more than a decade old, so it’s not just some passing fad or unstable prototype. And everyone will be pleased with the Ruby community, which is notably friendly.
Page
15
HOW THE BOOK WORKS 15 1.1 How the Book Works This is a hands-on book. Scripting is like riding a bicycle: you don’t learn it by reading about it; you learn it by doing it. And you get better by doing more of it. The purpose of a book, or of a coach, is to direct your practice so that you get better faster. Therefore, the book is organized around four separate projects that are similar to those you might do in real life. I build the first two projects slowly, showing and explaining all my work. You’ll learn best if you type along with me, building the project as we go. In the third and fourth projects, I move faster and explain only the finished result. The practice files that come with the book contain a series of snapshots practice files for each of the first two projects. The snippets of Ruby code in the book identify the file they come from. You can look at the file to see the snippet in context, to diagnose problems by comparing what you’ve typed to what I have, or to start your own typing in the middle of a project instead of at the beginning. Some of you won’t create the projects along with me. I do still urge you to work through the exercises and compare your solutions to the solutions I give. The Projects The first project is an uninstaller checker. If you uninstall your com- pany’s product, does the uninstaller remove everything it should? Does it remove something it shouldn’t? This script will tell you. More gen- erally, it lets you take snapshots of any part of your hard disk and compare them. The second project reaches out to a version control system, retrieves change information, and summarizes it for you. It’s a typical example of manipulating text. The third project visits to a website, “scrapes” data out of it, and puts that data into a comma-separated value file for use by a spreadsheet. The final project is a “watchdog” script. It can watch long-running pro- grams or tests and then send you an instant message or email when they finish.
Page
16
AN OUTLINE OF THE BOOK 16 A Special Note to Testers You were the original audience for this book. It used to be called Scripting for Testers, but people kept saying it would be useful to a broader audience. Even programmers I expected to be uninterested said things like “with only a few changes, this book would be for me.” So I made the changes, but testers still have a special place in my heart. As a tester, I bet you came to this book hoping to learn how to automate test execution: how to push inputs at a program (probably through the user interface), collect the results, and compare what the program produced to what it should have produced. Even when this book was exclusively for testers, I didn’t create any projects like that. I had two reasons: • Automating test execution is not the most efficient way for you to learn. I aim to teach you the practices, habits, and Ruby features you’ll need in real life. You don’t need those things to write one automated test or even ten, maybe not even a hundred, so it would feel artificial, false, and unconvincing for me to teach them in the context of a small automated test suite. They’re better taught with small projects of a different sort. • Automating test execution may not be the most effective thing for you to do. Is test execution the only task you do by hand? Probably not. People overly focused on test automation often miss opportunities for simple scripts that yield outsized improvements. 1.2 An Outline of the Book This is a book about both the features of Ruby and the craft of scripting. Each part of the book teaches some of both. Ruby features are intro- duced as they’re needed for that part’s project. Each part also intro- duces new skills that build on earlier ones. Part I, on page 30, teaches you the basics of Ruby and the basics of scripting. If you’ve never programmed, work through it carefully. If you already know a language, you can read it more casually, but do still read it. Ruby is based on ideas you might not know and has features you may not have seen before; if you skip them, you won’t be prepared for the rest of the book.
Page
17
SERVICE AFTER THE SALE 17 At the end of Part I, all three kinds of reader will be ready to learn how to script better. Part II, on page 67, adds more Ruby facts, but it’s mainly about teaching you how to write scripts in a steady, controlled way. All programmers know the feeling of hitting that wall where they can’t make any change without breaking something. I want to show you how to push that wall further away. Part III, on page 139, concentrates on accomplishing more with less effort. It shows how to save work by finding, understanding, and includ- ing libraries written by others. It shows you how to set up your scripts so that your co-workers can download, install, and use them easily. While demonstrating still more features of Ruby, this part also elabo- rates on an important topic from Part II, “regular expressions,” a pow- erful way of searching text. Part IV, on page 213, covers the advanced topic of inheritance. Inheri- tance can sometimes save even more work than libraries because some- one else designs a framework for part of your script. You need only plug in pieces that the framework orchestrates. Part IV shows you both how to use complicated frameworks others create and how to make simpler ones for yourself. You may want to get experience writing scripts of your own before learning about frameworks. The book ends with a glossary, solutions to exercises, and an index. What else? Throughout the book, you’ll find chapters called “Ruby Facts.” When I introduce a Ruby feature in the process of creating a script, I’ll describe only the bits used in the script we’re writing. But you’ll want to know more about such features when you write your own scripts, so I use the fact chapters to tell you more. Skip them if you like. Despite those chapters, this book is not a complete reference on Ruby. Eventually you’ll want to buy one. I heartily recommend Dave Thomas and friends’ Programming Ruby [TH01]. It’s also from the Pragmatic Bookshelf—indeed, Dave is one of the owners of the press. But I’m not recommending their book because they’re my publisher. They’re my publisher because I kept recommending their book. 1.3 Service After the Sale Everyday Scripting with Ruby has its very own Pragmatic Programmers’ web page at http://www.pragmaticprogrammer.com/titles/bmsft/. There, you will find updates, errata, source for all the examples and more.
Page
18
SUPPLEMENTS 18 1.4 Supplements As time and demand permit, I’ll be publishing supplements to this book; each will be devoted to a particular topic. Please check the book’s home page for details. 1.5 Acknowledgments This book would not exist were it not for the prodding of Bret Pettichord. Thank you, those who commented on drafts: Mark Axel, Tracy Beeson, Michael Bolton, Paul Carvalho, Tom Corbett, Bob Corrick, Lisa Crispin, Paul Czyzewski, Shailesh Dongre, Gunjan Doshi, Danny Faught, Zeljko Filipin, Pierre Garique, George Hawthorne, Paddy Healey, Jonathan Kohl, Bhavna Kumar, Walter Kruse, Jody Lemons, Iouri Makedonov, Chris McMahon, Christopher Meisenzahl, Grigori Melnik, Sunil Menda, Jack Moore, Erik Petersen, Bret Pettichord, Alan Richardson, Paul Rogers, Tony Semana, Kevin Sheehy, Jeff Smathers, Mike Stok, Paul Szymkowiak, Jonathan Towler, and Glenn Vanderburg. Special thanks to Paul Carvalho for teaching me something I didn’t know about Windows and for working through Part IV before Part III, and to Paul Czyzewski for how thoroughly he reviewed the pages I gave him time to review. My editor, Daniel Steinberg, provided just the right mix of encourage- ment, support, and pressure. I’ll be eternally grateful to my publishers, Andy Hunt and Dave Thomas, for not seeming to mind as their children were born, grew up, left home, got married, and had children of their own—all during the writing of this book. And I’d like to thank my family. You wouldn’t believe what they’ve let me get away with.
Page
19
Chapter 2 Getting Started This chapter gets you ready for the rest of the book. • Everyone will need to download the practice files. • If you’re not familiar with the command line (“the DOS prompt,” “the shell”), you’ll need to learn a bit about it. • Ruby might be preinstalled on your system. If it isn’t, you’ll need to install it. • Anytime you type, you make typographical errors. Typing scripts is no different. You need to learn to recognize the signs you’ve made a mistake. 2.1 Download the Practice Files This book comes with a number of Ruby scripts you can practice on. You can download them as a zip archive from the book’s web page at http://www.pragmaticprogrammer.com/titles/bmsft/. Download it anywhere you please. Your browser might “unzip” the file for you when you download it. If not, double-clicking or right-clicking it will probably work. Failing that, on Mac OS X and other Unix variants you can type unzip bmsft-code.zip to the command-line interpreter.1 On Windows, download an application like WinZip (http://winzip.com/), and set it to work. Unzipping the file creates a folder named code. I recommend renaming that to something more specific, like scripting-book, but I’ll use code to refer to it throughout the book. 1. The command-line interpreter will be explained shortly.
Page
20
IN THE BEGINNING WAS THE COMMAND LINE 20 Within code, there is a subfolder for each of the scripts in the book. Do your work within those subfolders. There’s also a subfolder with solu- tions to the exercises and several subfolders with more Ruby examples. 2.2 In the Beginning Was the Command Line When you use Ruby or any other scripting language, you’re likely to use your computer’s command-line interpreter. The command-line2 inter- command-line interpreter preter is a program that lets you command the compiler by typing in text, rather than by pointing and clicking with a mouse. If you’ve never used the command-line interpreter, here’s an introduction. Windows In Windows, you get to the command-line interpreter from the Start menu. Click the Run menu item, type cmd, and then press Enter . You’ll see something like this: Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:> The C:\> you see is called the prompt. It’s called that because it’s sup- posed to prompt you to type some commands for the computer to exe- cute. Not everyone who reads this book will have the same prompt, so I’ve arbitrarily chosen to show the prompt as prompt> from now on, except when I’m talking about something specific to Windows. When you see an instruction to type something like this: prompt> irb I want you to type i r b Enter . Don’t type the prompt. Let’s suppose you installed the practice files in C:\unzip-place. Type this: C:\> cd c:\unzip-place\code (cd stands for “change directory”—“directory” is a synonym for “folder”.) When you change to a folder, it becomes your current working folder. If a current working folder command doesn’t name a specific folder, the command-line interpreter assumes you mean the current working folder. For example, you can view the contents of the current working folder like this: C:\unzip-place\code\> dir 2. The title of this section refers to an essay by author Neal Stephenson. You can find it at http://www.cryptonomicon.com/beginning.html.