Programming Ruby 1.9 pptx

936 994 0
Programming Ruby 1.9 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info Progr amming Ruby 1.9 The Pragmatic Programmers’ Guide Dave Thomas with Chad Fowler and Andy Hunt The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas www.it-ebooks.info Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. W here those designations appear in this book, a nd 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 r evised 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 © 2 0 09 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be repro- duced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopy- ing, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN: 1-934356-08-5 ISBN-13: 978-1-934356-08-1 Text printed on acid-free paper. www.it-ebooks.info Contents FO REWORD 16 PREFACE 17 ROAD MAP 22 PART I —FACETS OF RUBY 1 GETTING STARTED 25 The Command Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Installing Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Running Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Ruby Documentation: RDoc and ri . . . . . . . . . . . . . . . . . . . . . . . . 32 2 RUBY.N EW 35 Ruby Is an Object-Oriented Language . . . . . . . . . . . . . . . . . . . . . . 35 Some Basic Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Arrays and Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Blocks and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Reading and ’Riting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Onward and Upward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3 CLASSES, OBJECTS, AND VARIABLES 50 Objects and Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Classes Working with Other Classes . . . . . . . . . . . . . . . . . . . . . . . 58 Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 4 www.it-ebooks.info CONTENTS 5 4 CONTAINERS, BLOCKS, AND ITERATORS 67 Blocks and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Containers Everywhere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 5 SHARING FU NCTIONALITY: INHERITANCE, MODULES, AND MIXINS 91 Inheritance and Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Mixins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Iterators and the Enumerable Module . . . . . . . . . . . . . . . . . . . . . . . 100 Composing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Inheritance, Mixins, and Design . . . . . . . . . . . . . . . . . . . . . . . . . 104 6 STANDARD TYPES 106 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 7 REGULAR EX PRESSIONS 117 What Regular Expressions Let You Do . . . . . . . . . . . . . . . . . . . . . . 117 Ruby’s Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Digging Deeper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Pattern-Based Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Advanced Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 130 8 MORE ABOUT METHODS 137 Defining a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Calling a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 9 EXPRESSIONS 1 46 Operator Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Miscellaneous Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Case Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Variable Scope, Loops, and Blocks . . . . . . . . . . . . . . . . . . . . . . . . 165 10 EXCEPTIONS, CATCH, AND THROW 167 The Exception Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Raising Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Catch and Throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 11 BASIC INPUT AND OUTPUT 176 What Is an IO Object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Opening and Closing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Talking to Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 www.it-ebooks.info CONTENTS 6 1 2 FIBERS, THREADS, AND PROCESSES 184 Fibers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Controlling the Thread S cheduler . . . . . . . . . . . . . . . . . . . . . . . . . 190 Mutual Exclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Running Multiple Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 13 UNIT TE STING 198 The Testing Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Structuring Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Organizing and Running Tests . . . . . . . . . . . . . . . . . . . . . . . . . . 206 RSpec and Shoulda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 14 WHEN TROUBLE STRIKES 220 Ruby Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Interactive Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Editor Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 But It Doesn’t Work! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 But It’s Too Slow! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 PART I I—RUBY IN ITS SETTING 15 RUBY AND ITS WORLD 233 Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Program Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Where Ruby Finds Its Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . 238 RubyGems Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 The Rake Build Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Build Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 16 NAMESPACES, SOURCE FILES, AND DISTRIBUTION 249 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Organizing Your Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Distributing and Installing Your Code . . . . . . . . . . . . . . . . . . . . . . 258 17 CHARACTER EN CODING 264 Encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Transcoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Input and Output Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Default External Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Encoding Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Default Internal Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Fun with Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 www.it-ebooks.info CONTENTS 7 1 8 INTERACTIVE RUBY SHELL 278 Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 19 DOCUMENTING RU BY 290 Adding RDoc to Ruby Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Adding RDoc to C Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . 298 Running RDoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 20 RUBY AND THE WEB 304 Writing CGI Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 Choice of Web Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 21 RUBY AND MI CROSOFT WINDOWS 316 Getting Ruby for Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Running Ruby Under Windows . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Win32API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Windows Automation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 PART III—RUBY CRYSTALLIZED 22 THE RU BY LANGUAGE 325 Source File Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 Source Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 The Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Method Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 Invoking a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Module Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 Blocks, Closures, and Proc Objects . . . . . . . . . . . . . . . . . . . . . . . . 363 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Catch and Throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 www.it-ebooks.info CONTENTS 8 2 3 DUCK TYPING 370 Classes Aren’t Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Coding like a Duck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 Standard Protocols and Coercions . . . . . . . . . . . . . . . . . . . . . . . . 376 Walk the Walk, Talk the Talk . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 24 METAPROGRAMMING 3 84 Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 Singletons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 Inheritance and Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 Modules and Mixins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 Metaprogramming Class-Level Macros . . . . . . . . . . . . . . . . . . . . . . 397 Two Other Forms of Class Definition . . . . . . . . . . . . . . . . . . . . . . . 402 instance_eval and class_eval . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 Hook Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 One Last Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 Top-Level Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . 417 The Turtle Graphics Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 25 REFLECTION, OBJECTSPACE, AND DISTRIBUTED RUBY 420 Looking at Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 Looking at Class es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422 Calling Methods Dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . 423 System Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426 Tracing Your Program’s Execution . . . . . . . . . . . . . . . . . . . . . . . . 427 Behind the Curtain: The Ruby VM . . . . . . . . . . . . . . . . . . . . . . . . 430 Marshaling and Distributed Ruby . . . . . . . . . . . . . . . . . . . . . . . . . 431 Compile Time? Runtime? Anytime! . . . . . . . . . . . . . . . . . . . . . . . 435 26 LOCKING RU BY IN THE SAFE 436 Safe Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 Tainted Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438 Trusted Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438 PART I V—R UBY LIBRARY REFERENCE 27 BUILT-IN CLASSES AND MODULES 442 Alphabetical Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 BasicObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 Bignum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469 Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470 Comparable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 Complex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 www.it-ebooks.info CONTENTS 9 D ir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478 Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Enumerable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 Enumerator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496 Errno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500 Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501 FalseClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504 Fiber . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 File::Stat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518 FileTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524 Fixnum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525 Float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528 GC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532 Hash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533 Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546 Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564 Marshal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 MatchData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585 Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591 Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594 Mutex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612 NilClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613 Numeric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615 Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622 ObjectSpace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635 Proc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637 Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641 Process::GID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648 Process::Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650 Process::Sys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 Process::UID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655 Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656 Rational . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660 Regexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663 Signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668 String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670 Struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696 Struct::Tms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700 Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701 Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705 ThreadGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712 Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714 TrueClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723 www.it-ebooks.info CONTENTS 10 U nboundMethod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724 28 STANDARD LIBRARY 726 Library Changes in Ruby 1.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . 727 Abbrev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729 Base64 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730 Benchmark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731 BigDecimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732 CGI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733 CGI::Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735 CMath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736 Complex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737 Continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738 CSV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739 Curses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741 Date/DateTime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742 DBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743 Delegator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744 Digest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745 DL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746 dRuby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747 English . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748 erb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749 Etc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751 expect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752 Fcntl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753 Fiber . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754 FileUtils . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755 Find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756 Forwardable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757 GDBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758 GetoptLong . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759 GServer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760 Iconv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761 IO/Wait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762 IPAddr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763 irb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764 json . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765 Logger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766 mathn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767 Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769 MiniTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770 Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771 Mutex_m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772 Net::FTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773 Net::HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774 Net::IMAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776 www.it-ebooks.info [...]... following command installs Ruby, irb, and ri: $ sudo apt-get install ruby1 .9 libruby1.9 libreadline -ruby1 .9 irb1.9 $ sudo apt-get install rdoc1.9 ri1.9 This installs all the Ruby commands with a 1.9 suffix, so you’ll need to do this: $ ruby1 .9 -v ruby 1.9. 0 (2007-12-25 revision 14709) [i486-linux] Be aware that the version of Ruby we just installed is many months behind the current version Note that you... a second and consider switching to Ruby 1.9 As a language, and as a programming environment, it really is a step up from previous versions of Ruby It runs faster, it is more expressive, and it enables even more programming paradigms Most frameworks (including Ruby on Rails) are now compatible with Ruby 1.9 And some Ruby implementations (such as MacRuby—a version of Ruby that is integrated into the Objective... the meantime, you can download MacRuby from http://www.macruby.org 3 http://www.macports.org/ www.it-ebooks.info I NSTALLING RUBY 29 the basic ports system installed, as described on its website, installing Ruby is as simple as doing this: $ sudo port install ruby1 9 As with apt-get for Linux, MacPorts currently installs the Ruby executables with a 1.9 suffix (ruby1 .9, irb1.9, and so on) If you don’t already... Ruby 1.8 preinstalled.2 If you want to make use of the new Ruby 1.9 features, you’ll want to install Ruby yourself You can do this from source, or you can use a package management system I personally use MacPorts.3 Once you have 1 Visit http://www .ruby- lang.org/en/downloads/, and look for Ruby on Windows 2 At some point, it seems likely that Apple will include MacRuby This is its own port of Ruby 1.9, ... character encodings, making Ruby one of the only programming languages to live fully in the whole world Given a choice between showing the 1.8 version of some Ruby construct and the 1.9 version, this book shows the new way If you’re planning to use Ruby 1.8 and not Ruby 1.9, then I’d recommend putting this book down and instead looking at the second edition of Programming Ruby. 1 But, before you run... the alternatives www.it-ebooks.info RUBY D OCUMENTATION : RD OC AND RI 34 Reissue the command, prefixing the method name with the name of the class and a dot: $ ri assoc More than one method matched your request You can refine your search by asking for information on one of: Array#assoc [Ruby 1.9. 1] Array#rassoc [Ruby 1.9. 1] Hash#assoc [Ruby 1.9. 1] Hash#rassoc [Ruby 1.9. 1] $ ri Array.assoc ... included in the Ruby distribution) has grown tremendously The community now has a standard documentation system (RDoc), and RubyGems has become the system of choice for packaging Ruby code for distribution We have a best-of-breed web application framework, Ruby on Rails, with others waiting in the wings Ruby Versions This version of the PickAxe documents Ruby 1.9. 2 Exactly what version of Ruby did I use... installing Ruby 1.9 on Windows You can visit http://rubyforge.org/proj and see whether a one-click installer is available If not, you can download a prebuilt binary from ruby- lang.org.1 Linux Distributions Most modern Linux distributions use the apt-get system (or the Synaptic GUI) to find and install Ruby As of November 2008, the following command installs Ruby, irb, and ri: $ sudo apt-get install ruby1 .9... Enjoy Yukihiro Matsumoto, a.k.a “Matz” Japan, February 2009 16 www.it-ebooks.info Preface This book is a new version of the PickAxe, as Programming Ruby is known to Ruby programmers It is a tutorial and reference for the version 1.9 of Ruby programming language Ruby 1.9 is a significant departure from previous versions There are major changes in string handling, the scoping of block variables, and the... One-Click Installer, or OCI However, with the advent of Ruby 1.9, the situation has changed somewhat Ruby 1.9 hasn’t been around long, so some of the libraries that were included in the 1.8 installer have not yet been made compatible with 1.9 As I write this, the OCI project is in a state of flux The maintainer, Luis Lavena, is planning on releasing a Ruby 1.9 version of the OCI in early 2009, but it may well . documents Ruby 1. 9. 2 Exactly what version of Ruby did I use to write this book? Let’s as k Ruby: % ruby -v r uby 1. 9. 1p0 (20 09- 01- 30 revision 2 19 07) [i386-darwin9.6.0] This. America. ISBN: 1- 93 4356-08-5 ISBN -13 : 97 8 -1- 93 4356-08 -1 Text printed on acid-free paper. www.it-ebooks.info Contents FO REWORD 16 PREFACE 17 ROAD MAP 22 PART

Ngày đăng: 23/03/2014, 02:20

Từ khóa liên quan

Mục lục

  • Foreword

  • Preface

  • Road Map

  • Part I---Facets of Ruby

    • Getting Started

      • The Command Prompt

      • Installing Ruby

      • Running Ruby

      • Ruby Documentation: RDoc and ri

      • Ruby.new

        • Ruby Is an Object-Oriented Language

        • Some Basic Ruby

        • Arrays and Hashes

        • Symbols

        • Control Structures

        • Regular Expressions

        • Blocks and Iterators

        • Reading and 'Riting

        • Command-Line Arguments

        • Onward and Upward

        • Classes, Objects, and Variables

          • Objects and Attributes

          • Classes Working with Other Classes

          • Access Control

Tài liệu cùng người dùng

Tài liệu liên quan