
Learning Ruby Other resources from O’Reilly Related titles Ruby Cookbook™ Ajax on Rails Ruby in a Nutshell Rails Cookbook™ Ruby on Rails: Up and Running oreilly.com oreilly.com is more than a complete catalog of O’Reilly books. You’ll also find links to news, events, articles, weblogs, sample chapters, and code examples. oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, pro- gramming languages, and operating systems. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries.We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches.Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals.Conduct searches across more than 1,000 books.Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today for free. Learning Ruby Michael Fitzgerald Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo Learning Ruby by Michael Fitzgerald Copyright © 2007 Michael Fitzgerald. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editor: Simon St.Laurent Cover Designer: Karen Montgomery Production Editor: Lydia Onofrei Interior Designer: David Futato Proofreader: Lydia Onofrei Illustrators: Robert Romano and Jessamyn Read Indexer: John Bickelhaupt Printing History: May 2007: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning Ruby, the image of a giraffe, and related trade dress are trademarks of O’Reilly Media, Inc. 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 O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN-10: 0-596-52986-4 ISBN-13: 978-0-596-52986-4 [M] Robert Wayne Darrah 1950–2006 Till we meet again Table of Contents Preface . xi 1. Ruby Basics . 1 Hello, Matz 2 Interactive Ruby 12 Resources 13 Installing Ruby 15 Permission Denied 20 Associating File Types on Windows 21 Review Questions 22 2. A Quick Tour of Ruby . 23 Ruby Is Object-Oriented 23 Ruby’s Reserved Words 26 Comments 27 Variables 28 Strings 30 Numbers and Operators 33 Conditional Statements 34 Arrays and Hashes 34 Methods 35 Blocks 40 Symbols 43 Exception Handling 44 Ruby Documentation 45 Review Questions 45 vii 3. Conditional Love . 47 The if Statement 47 The case Statement 51 The while Loop 52 The loop Method 55 The for loop 56 Execution Before or After a Program 59 Review Questions 59 4. Strings . 60 Creating Strings 60 Concatenating Strings 63 Accessing Strings 63 Comparing Strings 65 Manipulating Strings 66 Case Conversion 70 Managing Whitespace, etc. 71 Incrementing Strings 73 Converting Strings 74 Regular Expressions 74 1.9 and Beyond 78 Review Questions 78 5. Math . 79 Class Hierarchy and Included Modules 80 Converting Numbers 81 Basic Math Operations 81 Ranges 85 Inquiring About Numbers 86 More Math Methods 88 Math Functions 88 Rational Numbers 90 Prime Numbers 91 Review Questions 92 6. Arrays . 93 Creating Arrays 94 Accessing Elements 97 Concatenation 99 viii | Table of Contents Set Operations 99 Unique Elements 100 Blow Your Stack 100 Comparing Arrays 101 Changing Elements 101 Deleting Elements 103 Arrays and Blocks 103 Sorting Things and About Face 104 Multidimensional Arrays 104 1.9 and Beyond 105 Other Array Methods 105 Review Questions 105 7. Hashes . 107 Creating Hashes 107 Accessing Hashes 108 Iterating over Hashes 109 Changing Hashes 110 Converting Hashes to Other Classes 113 1.9 and Beyond 113 Other Hash Methods 114 Review Questions 114 8. Working with Files . 115 Directories 115 Creating a New File 117 Opening an Existing File 117 Deleting and Renaming Files 120 File Inquiries 120 Changing File Modes and Owner 121 The IO Class 123 Review Questions 124 9. Classes . 125 Defining the Class 126 Instance Variables 127 Accessors 129 Class Variables 130 Class Methods 131 Table of Contents | ix Inheritance 133 Modules 134 public, private, or protected 137 Review Questions 138 10. More Fun with Ruby . 139 Formatting Output with sprintf 139 Processing XML 142 Date and Time 147 Reflection 151 Using Tk 154 Metaprogramming 157 RubyGems 158 Exception Handling 162 Creating Documentation with RDoc 164 Embedded Ruby 171 Review Questions 174 11. A Short Guide to Ruby on Rails . 175 Where Did Rails Come From? 175 Why Rails? 176 What Have Other Folks Done with Rails? 180 Hosting Rails 182 Installing Rails 182 Learning Rails 185 A Brief Tutorial 186 Review Questions 193 A. Ruby Reference . 195 B. Answers to Review Questions . 214 Glossary . 219 Index . 227 x | Table of Contents Preface1 Ruby has gotten a lot of attention since the appearance of Ruby on Rails, the web application framework written in Ruby.The attention is way past due.Ruby has been around as long as Java but enjoyed only limited attention outside of Japan until around 2000.In the last few years, Ruby’s popularity has steadily grown, and with good reason. Who Should Read This Book? Generally, I figure two kinds of readers will buy this book: experienced program- mers who want to learn Ruby, and new programmers who want to learn to program. I have the interesting job of catering to both while trying not to tick off either.It’s a balancing act, and this is how I’ll handle it: I am going to address you as if you are already a competent programmer, but I’ll also provide plenty of help for beginners, mostly in the form of notes or sidebars.I’ll let you know when you can skip a sec- tion if you are already a heavy hitter. If you’re a fairly experienced programmer, you might just want to read the code examples first, from the beginning of the book to the end, skimming the explana- tions surrounding the examples as needed.You should be able to see what’s going on fairly quickly just by keeping your eyes on the code.The code is laid out in a more or less logical fashion (to me at least), so you should be able to figure out Ruby in fairly short order.If you are new to programming, I have attempted to make your job a little easier by explaining things as I go along. How This Book Works Do you have to know everything about a car before you start driving? Did you have to know anything about fuel injection, combustion, or timing belts to drive? Of course not. xi It’s the same with programming in a new language.I am going to show you lots of Ruby programs, many of them just one-liners, and then tell you how and why they work—just enough to get you rolling down the road.I take this approach because I believe we do most of our learning by observing, imitating, and playing.I plan to do a lot of that in this book. You should know up front that this is a just-get-in-and-drive book.In other words, you can drive a car even if you don’t know whether its got six or eight cylinders. David Heinemeier Hansson, inventor of Ruby on Rails, said something I like: “Peo- ple learn by changing a little thing, reloading, and seeing the change.” He’s right on. That’s my experience: over the years I have learned more by hacking code than by reading about it. I also move as quickly as possible, not getting bogged down in the quicksand of details.The details will come in time, as they are needed; the main thing I want to give you now is forward movement and momentum. If you just follow along with what I’m doing, running the programs and altering them to your taste, you’ll learn quickly.The more you run these programs, the more fluency you’ll develop, and before long, you’ll start thinking and even dreaming in Ruby. Then you’ll just take off on your own. The latest stable version at the time I am writing this is 1.8.6. That’s the version I’ll be using.You can probably get along using an older version, but unless you have 1.8.6 or later installed, I can’t guarantee that all the programs in this book will work as advertised, though they most likely will. About the Examples I think we learn best by observing what others do, then imitating what we observe. That’s how we learn as children, anyway.And that’s why you’ll find code exam- ples—to observe and imitate—on nearly every page of this book. Many of the examples are available for download from http://www.oreilly.com/ catalog/9780596529864.The idea is that you will have enough examples in your hands to start most of the basic programming tasks.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages257 Page
-
File Size-