ebook img

Programming Ruby PDF

833 Pages·2006·5.44 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming Ruby

Prepared exclusively for Dr. Eugene Wallingford Developers the world over talk about Programming Ruby and the Ruby language. . . “Ruby is a wonderfully powerful and useful language, and whenever I’m working with it, this book is at my side.” Martin Fowler, Chief Scientist, ThoughtWorks “If your world revolves around Java, as mine did, then you need this outstanding book to learn all the wonderful things you’re missing. There’s just one catch: you’ll be spoiled from then on. Indeed, after reading just a few pages of Programming Ruby, programming in any language other than Ruby will feel like you’re pushing rope.” Mike Clark, Author and Consultant “Ruby is smart, elegant, and fun, and it deserves a book that’s smart, elegant, and fun. The first edition of Programming Ruby was such a book; the second edition is even better.” James Britt, Administrator, http://ruby-doc.org “The best reason to learn a new programming language is to learn to think differently. The best way to learn to think the Ruby way is to read Programming Ruby. Several years ago, with the first edition of this book, I did just that. Since then, I’ve had a constant stream of enjoyable Ruby programming experiences. This is due in no insignificant part to the quality of the source from which I learned the language. I’m not the only person I’ve heard say that every language should have a book like this.” Chad Fowler, Codirector, Ruby Central, Inc. “The PickAxe got me started on Ruby. It is still the first book I turn to.” Ryan Davis, Founder, Seattle.rb “This book changed my life. Sounds rather clichéd, but it’s the truth. After six years and 300,000 lines of Java code, I needed a change. That change occurred upon reading the first edition of this book. With the support of a solid community and ever-growing foundation of superb libraries, I founded a company that largely profits from applying Ruby to solve real-world problems. Ruby is ready for prime time, and this new version of the PickAxe will show a waiting world what a gem Ruby really is.” Rich Kilmer, President and CEO, InfoEther LLC “The first edition of PickAxe has been a desk-side companion for years. The second edition will be an eagerly awaited replacement.” Tom Enebo, JRuby Developer Prepared exclusively for Dr. Eugene Wallingford “The first edition of Programming Ruby brought about no less than the introduction of Ruby on a large scale outside of Japan, in the process becoming the de facto standard published language reference and an oft-cited model of clear, effective technical writing. The appearance of the second, expanded edition is exciting for Ruby programmers around the world and will no doubt attract a fresh wave of newcomers to this elegant, versatile language.” David A. Black, Ph.D., Codirector, Ruby Central, Inc. “Ruby is my definite choice for all scripting and prototyping issues, and this book will help you to discover its usefulness as well as its beauty. Apart from that, it’s really fun to read!” Robert Klemme “I bought the first edition of this book the day it was released and had a fantastic time using it to learn Ruby. I eventually bought a second copy to keep at home. But Ruby has changed since then. I’m delighted that this second edition of Programming Ruby is available to help a new round of programmers learn about this fantastic, beautiful language. And it’s not just good news for Ruby newbies, of course—like me, most Ruby developers will want a copy (no, make that two) so that all of the details about today’s Ruby will be close at hand.” Glenn Vanderburg, Software Architect, Countrywide Financial “Ruby is one of those great languages that takes an afternoon to start using and years (maybe a lifetime) to master. In C, I’m always having to work around the limitations of the language; in Ruby, I’m always discovering a neater, cleaner, more efficient way to do things. Programming Ruby is the essential reference to the Ruby language. More than just teaching you the syntax, it teaches you the spirit and the feel of the language.” Ben Giddings “Confucius said, “What you hear, you forget.” He also said, “What you do you understand.” But it’s not easy to actually “do” things unless you’re using a great language with strength in quick and clean prototyping. In my case, this language is Ruby! Thank you!” Michael Neumann Prepared exclusively for Dr. Eugene Wallingford Programming Ruby The Pragmatic Programmers’ Guide Second Edition Dave Thomas with Chad Fowler and Andy Hunt The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Prepared exclusively for Dr. Eugene Wallingford Many of the designations used by manufacturers and sellers to distinguish their products 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. 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. This book is a heavily revised version of the book Programming Ruby, originally published by Addison Wesley. This book is printed with their permission. 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 © 2005 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, 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 0-9745140-5-5 Text printed on acid-free paper. Tenth Printing, October 2006 Version: 2006-9-20 Prepared exclusively for Dr. Eugene Wallingford Contents FOREWORD TO THE FIRST EDITION xvii FOREWORD TO THE SECOND EDITION xix PREFACE xx ROAD MAP xxvi PART I—FACETS OF RUBY 1 GETTING STARTED 2 Installing Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Running Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Ruby Documentation: RDoc and ri . . . . . . . . . . . . . . . . . . . . . 7 2 RUBY.NEW 9 Ruby Is an Object-Oriented Language . . . . . . . . . . . . . . . . . . . 9 Some Basic Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Arrays and Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Blocks and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Reading and ’Riting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Onward and Upward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3 CLASSES, OBJECTS, AND VARIABLES 23 Inheritance and Messages . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Objects and Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Class Variables and Class Methods . . . . . . . . . . . . . . . . . . . . . 31 Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Prepared exclusively for Dr. Eugene Wallingfordv CONTENTS vi 4 CONTAINERS, BLOCKS, AND ITERATORS 40 Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Blocks and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Containers Everywhere . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 5 STANDARD TYPES 55 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 6 MORE ABOUT METHODS 74 Defining a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Calling a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 7 EXPRESSIONS 81 Operator Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Miscellaneous Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 83 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Case Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Variable Scope, Loops, and Blocks . . . . . . . . . . . . . . . . . . . . . 99 8 EXCEPTIONS, CATCH, AND THROW 101 The Exception Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Raising Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Catch and Throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 9 MODULES 110 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Mixins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Iterators and the Enumerable Module . . . . . . . . . . . . . . . . . . . . 113 Composing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Including Other Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 10 BASIC INPUT AND OUTPUT 119 What Is an IO Object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Opening and Closing Files . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Talking to Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Prepared exclusively for Dr. Eugene Wallingford CONTENTS vii 11 THREADS AND PROCESSES 127 Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Controlling the Thread Scheduler . . . . . . . . . . . . . . . . . . . . . . 132 Mutual Exclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Running Multiple Processes . . . . . . . . . . . . . . . . . . . . . . . . . 139 12 UNIT TESTING 143 Test::Unit Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Structuring Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Organizing and Running Tests . . . . . . . . . . . . . . . . . . . . . . . . 151 13 WHEN TROUBLE STRIKES 155 Ruby Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Interactive Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Editor Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 But It Doesn’t Work! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 But It’s Too Slow! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 PART II—RUBY IN ITS SETTING 14 RUBY AND ITS WORLD 167 Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 167 Program Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Where Ruby Finds Its Modules . . . . . . . . . . . . . . . . . . . . . . . 172 Build Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 15 INTERACTIVE RUBY SHELL 174 Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 rtags and xmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 16 DOCUMENTING RUBY 187 Adding RDoc to Ruby Code . . . . . . . . . . . . . . . . . . . . . . . . . 187 Adding RDoc to C Extensions . . . . . . . . . . . . . . . . . . . . . . . . 195 Running RDoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Displaying Program Usage . . . . . . . . . . . . . . . . . . . . . . . . . 200 Prepared exclusively for Dr. Eugene Wallingford CONTENTS viii 17 PACKAGE MANAGEMENT WITH RUBYGEMS 203 Installing RubyGems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Installing Application Gems . . . . . . . . . . . . . . . . . . . . . . . . . 204 Installing and Using Gem Libraries . . . . . . . . . . . . . . . . . . . . . 206 Creating Your Own Gems . . . . . . . . . . . . . . . . . . . . . . . . . . 211 18 RUBY AND THE WEB 222 Writing CGI Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Improving Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Choice of Web Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 SOAP and Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 19 RUBY TK 241 Simple Tk Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Binding Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Translating from Perl/Tk Documentation . . . . . . . . . . . . . . . . . . 251 20 RUBY AND MICROSOFT WINDOWS 253 Getting Ruby for Windows . . . . . . . . . . . . . . . . . . . . . . . . . 253 Running Ruby Under Windows . . . . . . . . . . . . . . . . . . . . . . . 254 Win32API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Windows Automation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 21 EXTENDING RUBY 261 Your First Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Ruby Objects in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 The Jukebox Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Ruby Type System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Creating an Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Embedding a Ruby Interpreter . . . . . . . . . . . . . . . . . . . . . . . . 287 Bridging Ruby to Other Languages . . . . . . . . . . . . . . . . . . . . . 290 Ruby C Language API . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Prepared exclusively for Dr. Eugene Wallingford CONTENTS ix PART III—RUBY CRYSTALLIZED 22 THE RUBY LANGUAGE 302 Source Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 The Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Method Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 Invoking a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Module Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 Blocks, Closures, and Proc Objects . . . . . . . . . . . . . . . . . . . . . 341 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Catch and Throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 23 DUCK TYPING 349 Classes Aren’t Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 Coding like a Duck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 Standard Protocols and Coercions . . . . . . . . . . . . . . . . . . . . . . 355 Walk the Walk, Talk the Talk . . . . . . . . . . . . . . . . . . . . . . . . 361 24 CLASSES AND OBJECTS 362 How Classes and Objects Interact . . . . . . . . . . . . . . . . . . . . . . 362 Class and Module Definitions . . . . . . . . . . . . . . . . . . . . . . . . 370 Top-Level Execution Environment . . . . . . . . . . . . . . . . . . . . . 376 Inheritance and Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . 376 Freezing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 25 LOCKING RUBY IN THE SAFE 379 Safe Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 Tainted Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 26 REFLECTION, OBJECTSPACE, AND DISTRIBUTED RUBY 384 Looking at Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 Looking at Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 Calling Methods Dynamically . . . . . . . . . . . . . . . . . . . . . . . . 388 System Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 Tracing Your Program’s Execution . . . . . . . . . . . . . . . . . . . . . 393 Marshaling and Distributed Ruby . . . . . . . . . . . . . . . . . . . . . . 395 Compile Time? Runtime? Anytime! . . . . . . . . . . . . . . . . . . . . . 400 Prepared exclusively for Dr. Eugene Wallingford

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.