ebook img

Beginning Ruby From Novice to Professional PDF

658 Pages·2009·11.23 MB·English
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 Beginning Ruby From Novice to Professional

cyan yelloW MaGenTa Black panTone 123 c Books for professionals By professionals® The eXperT’s Voice® in open source companion NNooww ccoovveerrss eBook Available Beginning Ruby: From Novice to Professional, RRuubbyy 11..99!! SecoNd editioN Beginning Beginning Dear Reader, R Beginning Ruby is the only book you’ll need to go from not knowing Ruby at all to being proficient and confident enough to develop your own applications and learn more about the specific areas that interest you. With just this book, you’ll Ruby learn how to develop your own complete applications that can work online, u access databases, process files, and more. Thanks to the popularity of the Ruby on Rails web application framework, Ruby has rapidly become a major programming language in the last ten years, b roughly on a par with Python. Ruby is still growing and maturing, however, and now is an especially good time to learn it. I’ve written this book in a way that lets you quickly learn the language without being bamboozled with jargon, even if y you’ve only ever dabbled with programming before. I’ve also provided links to a multitude of online resources and documentation to help you progress further. My own Ruby learning experience is my motivation for writing this book. I wanted to write a book that didn’t assume you’re already a dynamic language or object-orientation expert. This book introduces the most basic of concepts, yet moves quickly enough that you won’t need to skip too much if you’re already familiar with certain areas. Most of the areas relevant to a professional Ruby developer are covered, too—even those not directly related to the language, From Novice to Professional such as database access and SQL. From the start, my ambition has been to write a book to educate and encour- age, rather than deliver dry facts. There are enough reference books out there, but very few that will show you how to program with Ruby in a guided, narrated way. This book not only shows you how to program with Ruby, it also teaches you how the Ruby community works, where to find help, and how to “walk the walk” and “talk the talk.” An instructional guide to the Ruby programming language. Peter Cooper Companion eBook SecoNd THE APRESS ROADMAP SeCoNd editioN editioN Practical Reporting with Ruby and Rails Beginning Ruby, Second Edition NetBeans™ Ruby See last page for details Scripting Intelligence and Rails IDE with JRuby on $10 eBook version Beginning Rails 3 Pulling Strings with Puppet SOURCE CODE ONLINE C Peter Cooper www.apress.com ISBN 978-1-4302-2363-4 o o 53999 p e Foreword by why the lucky stiff r US $39.99 Shelve in Ruby User level: 9 781430 223634 Beginner–Intermediate this print for content only—size & color not accurate spine = 1.2485" 656 page count Beginning Ruby From Novice to Professional S e econd dition Peter Cooper Beginning Ruby: From Novice to Professional, Second Edition Copyright © 2009 by Peter Cooper All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2363-4 ISBN-13 (electronic): 978-1-4302-2364-1 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Michelle Lowman Technical Reviewers: Alan Bradburne, Peter Szinek Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary C ornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Beth Christmas Copy Editors: Damon Larson, Candace English Associate Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor: Molly Sharp, ContentWorks Proofreader: Nancy Bell Indexer: Julie Grady Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http:// www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indi- rectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. For Laura and Penny Contents at a Glance Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi PART 1 n n n Foundations and Scaffolding ChaPtER 1 Let’s Get It Started: Installing Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 ChaPtER 2 Programming == Joy: A Whistle-Stop Tour of Ruby and Object Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 ChaPtER 3 Ruby’s Building Blocks: Data, Expressions, and Flow Control . . . . .31 ChaPtER 4 Developing Your First Ruby Application . . . . . . . . . . . . . . . . . . . . . . . . . . 77 ChaPtER 5 The Ruby Ecosystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99 PART 2 n n n The Core of Ruby ChaPtER 6 Classes, Objects, and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 ChaPtER 7 Projects and Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161 ChaPtER 8 Documentation, Error Handling, Debugging, and Testing . . . . . . . .177 ChaPtER 9 Files and Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 ChaPtER 10 Deploying Ruby Applications and Libraries . . . . . . . . . . . . . . . . . . . . .247 ChaPtER 11 Advanced Ruby Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275 ChaPtER 12 Tying It Together: Developing a Larger Ruby Application . . . . . . . .307 PART 3 n n n Ruby Online ChaPtER 13 Web Application Frameworks: Rails, Sinatra, and Ramaze . . . . . . . 349 ChaPtER 14 Ruby and the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .413 iv ChaPtER 15 Networking, Sockets, and Daemons . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 ChaPtER 16 GUI-Based Desktop Application Development . . . . . . . . . . . . . . . . . . . 467 ChaPtER 17 Useful Ruby Libraries and Gems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .495 aPPENDIX a Ruby Primer and Review for Developers . . . . . . . . . . . . . . . . . . . . . . . . 547 aPPENDIX B Ruby Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575 aPPENDIX C Useful Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .589 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .601 v Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi PART 1 n n n Foundations and Scaffolding ChaPtER 1 Let’s Get It Started: Installing Ruby . . . . . . . . . . . . . . . . . . . . . . . . .3 Installing Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Apple Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Other Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 ChaPtER 2 Programming == Joy: a Whistle-Stop tour of Ruby and Object Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Baby Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 irb: Interactive Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Ruby Is English for Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Why Ruby Makes a Great Programming Language . . . . . . . . . . . . . .15 Trails for the Mind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 Turning Ideas into Ruby Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 How Ruby Understands Concepts with Objects and Classes . . . . . . 17 The Making of a Man . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Basic Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 From People to Pets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20 vii viii nCONTENTS Everything Is an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Kernel Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 Passing Data to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25 Using the Methods of the String Class . . . . . . . . . . . . . . . . . . . . . . . . . 26 Using Ruby Without Object Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 ChaPtER 3 Ruby’s Building Blocks: Data, Expressions, and Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Numbers and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 Basic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32 Comparison Operators and Expressions . . . . . . . . . . . . . . . . . . . . . . .33 Looping Through Numbers with Blocks and Iterators . . . . . . . . . . . .35 Floating Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Text and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 String Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Regular Expressions and String Manipulation . . . . . . . . . . . . . . . . . . . 44 Arrays and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Basic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50 Splitting Strings into Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52 Array Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Other Array Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Basic Hash Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Hashes Within Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59 if and unless . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60 ?, the Ternary Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 elsif and case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62 while and until . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63 Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64 Other Useful Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 Large Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

Description:
Based on the best-selling first edition, Beginning Ruby: From Novice to Professional, Second Edition is the leading guide for every type of reader who wants to learn Ruby from the ground up. The new edition of this book provides the same excellent introduction to Ruby as the first edition plus updat
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.