Learning Ruby by Michael Fitzgerald Publisher: O'reilly Pub Date: May 01, 2007 Print ISBN-10: 0-596-52986-4 Print ISBN-13: 978-0-59-652986-4 Pages: 255
Total Page:16
File Type:pdf, Size:1020Kb
Learning Ruby by Michael Fitzgerald Publisher: O'Reilly Pub Date: May 01, 2007 Print ISBN-10: 0-596-52986-4 Print ISBN-13: 978-0-59-652986-4 Pages: 255 Copyright Dedication Preface Chapter 1. Ruby Basics Section 1.1. Hello, Matz Section 1.2. Interactive Ruby Section 1.3. Resources Section 1.4. Installing Ruby Section 1.5. Permission Denied Section 1.6. Associating File Types on Windows Section 1.7. Review Questions Chapter 2. A Quick Tour of Ruby Section 2.1. Ruby Is Object-Oriented Section 2.2. Ruby's Reserved Words Section 2.3. Comments Section 2.4. Variables Section 2.5. Strings Section 2.6. Numbers and Operators Section 2.7. Conditional Statements Section 2.8. Arrays and Hashes Section 2.9. Methods Section 2.10. Blocks Section 2.11. Symbols Section 2.12. Exception Handling Section 2.13. Ruby Documentation Section 2.14. Review Questions Chapter 3. Conditional Love Section 3.1. The if Statement Section 3.2. The case Statement Section 3.3. The while Loop Section 3.4. The loop Method Section 3.5. The for loop Section 3.6. Execution Before or After a Program Section 3.7. Review Questions Chapter 4. Strings Section 4.1. Creating Strings Section 4.2. Concatenating Strings Section 4.3. Accessing Strings Section 4.4. Comparing Strings Section 4.5. Manipulating Strings Section 4.6. Case Conversion Section 4.7. Managing Whitespace, etc. Section 4.8. Incrementing Strings Section 4.9. Converting Strings Section 4.10. Regular Expressions Section 4.11. 1.9 and Beyond Section 4.12. Review Questions Chapter 5. Math Section 5.1. Class Hierarchy and Included Modules Section 5.2. Converting Numbers Section 5.3. Basic Math Operations Section 5.4. Ranges Section 5.5. Inquiring About Numbers Section 5.6. More Math Methods Section 5.7. Math Functions Section 5.8. Rational Numbers Section 5.9. Prime Numbers Section 5.10. Review Questions Chapter 6. Arrays Section 6.1. Creating Arrays Section 6.2. Accessing Elements Section 6.3. Concatenation Section 6.4. Set Operations Section 6.5. Unique Elements Section 6.6. Blow Your Stack Section 6.7. Comparing Arrays Section 6.8. Changing Elements Section 6.9. Deleting Elements Section 6.10. Arrays and Blocks Section 6.11. Sorting Things and About Face Section 6.12. Multidimensional Arrays Section 6.13. 1.9 and Beyond Section 6.14. Other Array Methods Section 6.15. Review Questions Chapter 7. Hashes Section 7.1. Creating Hashes Section 7.2. Accessing Hashes Section 7.3. Iterating over Hashes Section 7.4. Changing Hashes Section 7.5. Converting Hashes to Other Classes Section 7.6. 1.9 and Beyond Section 7.7. Other Hash Methods Section 7.8. Review Questions Chapter 8. Working with Files Section 8.1. Directories Section 8.2. Creating a New File Section 8.3. Opening an Existing File Section 8.4. Deleting and Renaming Files Section 8.5. File Inquiries Section 8.6. Changing File Modes and Owner Section 8.7. The IO Class Section 8.8. Review Questions Chapter 9. Classes Section 9.1. Defining the Class Section 9.2. Instance Variables Section 9.3. Accessors Section 9.4. Class Variables Section 9.5. Class Methods Section 9.6. Inheritance Section 9.7. Modules Section 9.8. public, private, or protected Section 9.9. Review Questions Chapter 10. More Fun with Ruby Section 10.1. Formatting Output with sprintf Section 10.2. Processing XML Section 10.3. Date and Time Section 10.4. Reflection Section 10.5. Using Tk Section 10.6. Metaprogramming Section 10.7. RubyGems Section 10.8. Exception Handling Section 10.9. Creating Documentation with RDoc Section 10.10. Embedded Ruby Section 10.11. Review Questions Chapter 11. A Short Guide to Ruby on Rails Section 11.1. Where Did Rails Come From Section 11.2. Why Rails? Section 11.3. What Have Other Folks Done with Rails? Section 11.4. Hosting Rails Section 11.5. Installing Rails Section 11.6. Learning Rails Section 11.7. A Brief Tutorial Section 11.8. Review Questions Appendix A. Ruby Reference Section A.1. Ruby Interpreter Section A.2. Ruby's Reserved Words Section A.3. Operators Section A.4. Escape Characters Section A.5. Predefined Variables Section A.6. Global Constants Section A.7. Regular Expressions Section A.8. String Unpack Directives Section A.9. Array Pack Directives Section A.10. Sprintf Flags and Field Types Section A.11. File Tests Section A.12. Time Formatting Directives Section A.13. RDoc Options Section A.14. Rake Appendix B. Answers to Review Questions Section B.1. Review Questions Section B.2. Review Questions Section B.3. Review Questions Section B.4. Review Questions Section B.5. Review Questions Section B.6. Review Questions Section B.7. Review Questions Section B.8. Review Questions Section B.9. Review Questions Section B.10. Review Questions Section B.11. Review Questions Glossary About the Author Colophon Index Copyright 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 (http://safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editor: Simon St.Laurent Production Editor: Lydia Onofrei Proofreader: Lydia Onofrei Indexer: John Bickelhaupt Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrators: Robert Romano and Jessamyn Read 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. ISBN-10: 0-596-52986-4 ISBN-13: 978-0-596-52986-4 [M] Dedication Robert Wayne Darrah 1950-2006 Till we meet again Preface 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 programmers 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 section 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 explanations 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. 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: "People 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.