Learning Ruby

Book description

You don't have to know everything about a car to drive one, and you don't need to know everything about Ruby to start programming with it. Written for both experienced and new programmers alike, Learning Ruby is a just-get-in-and-drive book -- a hands-on tutorial that offers lots of Ruby programs and lets you know how and why they work, just enough to get you rolling down the road.

Interest in Ruby stems from the popularity of Rails, the web development framework that's attracting new devotees and refugees from Java and PHP. But there are plenty of other uses for this versatile language. The best way to learn is to just try the code! You'll find examples on nearly every page of this book that you can imitate and hack. Briefly, this book:

  • Outlines many of the most important features of Ruby
  • Demonstrates how to use conditionals, and how to manipulate strings in Ruby. Includes a section on regular expressions
  • Describes how to use operators, basic math, functions from the Math module, rational numbers, etc.
  • Talks you through Ruby arrays, and demonstrates hashes in detail
  • Explains how to process files with Ruby
  • Discusses Ruby classes and modules (mixins) in detail, including a brief introduction to object-oriented programming (OOP)
  • Introduces processing XML, the Tk toolkit, RubyGems, reflection, RDoc, embedded Ruby, metaprogramming, exception handling, and other topics
  • Acquaints you with some of the essentials of Rails, and includes a short Rails tutorial.
Each chapter concludes with a set of review questions, and appendices provide you with a glossary of terms related to Ruby programming, plus reference material from the book in one convenient location. If you want to take Ruby out for a drive, Learning Ruby holds the keys.

Publisher resources

View/Submit Errata

Table of contents

  1. Dedication
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Who Should Read This Book?
    2. How This Book Works
    3. About the Examples
    4. How This Book Is Organized
    5. Conventions Used in This Book
    6. Comments and Questions
    7. Safari® Enabled
    8. Acknowledgments
  4. 1. Ruby Basics
    1. Hello, Matz
      1. A Very Short Ruby Program
      2. Shebang!
      3. Issue a System Command
      4. Appending a String
      5. Multiply
      6. Inserting a Shell Command
      7. Using a Variable
      8. Expression Substitution
      9. Formatting a String
      10. The eval Method and -e Option
      11. Getting Input from the Keyboard
      12. Methods
      13. The block
      14. The each Method
      15. The proc
      16. XML
      17. The Class
      18. The Tk Toolkit
      19. Editing and Running Ruby in TextMate
    2. Interactive Ruby
    3. Resources
    4. Installing Ruby
      1. Installing Ruby on Mac OS X Tiger
      2. Installing Ruby on Windows with the One-Click Installer
      3. Installing Ruby on Windows with Binaries
      4. Installing Ruby on Linux
    5. Permission Denied
    6. Associating File Types on Windows
    7. Review Questions
  5. 2. A Quick Tour of Ruby
    1. Ruby Is Object-Oriented
      1. The Object Class and the Kernel Module
    2. Ruby’s Reserved Words
    3. Comments
    4. Variables
      1. Local Variables
      2. Instance Variables
      3. Class Variables
      4. Global Variables
      5. Constants
      6. Parallel Assignment
    5. Strings
      1. Regular Expressions
    6. Numbers and Operators
    7. Conditional Statements
    8. Arrays and Hashes
    9. Methods
      1. Return Values
      2. Method Name Conventions
      3. Default Arguments
      4. Variable Arguments
      5. Aliasing Methods
    10. Blocks
      1. The yield Statement
      2. Procs
    11. Symbols
    12. Exception Handling
    13. Ruby Documentation
    14. Review Questions
  6. 3. Conditional Love
    1. The if Statement
      1. Using else and elsif
      2. The Ternary Operator
    2. The case Statement
    3. The while Loop
      1.  
        1. Give me a break
      2. unless and until
    4. The loop Method
    5. The for loop
      1. The times Method
      2. The upto Method
      3. The downto Method
    6. Execution Before or After a Program
    7. Review Questions
  7. 4. Strings
    1. Creating Strings
      1. General Delimited Strings
      2. Here Documents
    2. Concatenating Strings
    3. Accessing Strings
    4. Comparing Strings
    5. Manipulating Strings
      1. Inserting a String in a String
      2. Changing All or Part of a String
      3. The chomp and chop Methods
      4. The delete Method
      5. Substitute the Substring
      6. Turn It Around
      7. From a String to an Array
    6. Case Conversion
      1. Iterating Over a String
      2. downcase, upcase, and swapcase
    7. Managing Whitespace, etc.
    8. Incrementing Strings
    9. Converting Strings
    10. Regular Expressions
    11. 1.9 and Beyond
    12. Review Questions
  8. 5. Math
    1. Class Hierarchy and Included Modules
    2. Converting Numbers
    3. Basic Math Operations
      1. Division and Truncation
      2. Equality, Less Than, or Greater Than
      3. Abbreviated Assignment Operators
      4. Operators
    4. Ranges
    5. Inquiring About Numbers
      1. Iterating Through Blocks
    6. More Math Methods
    7. Math Functions
    8. Rational Numbers
    9. Prime Numbers
      1. Just for Fun
    10. Review Questions
  9. 6. Arrays
    1. Creating Arrays
      1. Clear the Deck
      2. Creating an Array with a Block
      3. There’s an Easier Way
      4. It Gets Even Easier
    2. Accessing Elements
    3. Concatenation
    4. Set Operations
    5. Unique Elements
    6. Blow Your Stack
    7. Comparing Arrays
    8. Changing Elements
      1. As a String
      2. Using shift and unshift
    9. Deleting Elements
    10. Arrays and Blocks
    11. Sorting Things and About Face
    12. Multidimensional Arrays
    13. 1.9 and Beyond
    14. Other Array Methods
    15. Review Questions
  10. 7. Hashes
    1. Creating Hashes
    2. Accessing Hashes
    3. Iterating over Hashes
    4. Changing Hashes
      1. Merging Hashes
      2. Sorting a Hash
      3. Deleting and Clearing a Hash
      4. Replacing a Hash
    5. Converting Hashes to Other Classes
    6. 1.9 and Beyond
    7. Other Hash Methods
    8. Review Questions
  11. 8. Working with Files
    1. Directories
      1. Looking Inside Directories
      2. The Directory Stream
    2. Creating a New File
    3. Opening an Existing File
      1. ARGV and ARGF
      2. Opening a URI
    4. Deleting and Renaming Files
    5. File Inquiries
    6. Changing File Modes and Owner
    7. The IO Class
    8. Review Questions
  12. 9. Classes
    1. Defining the Class
    2. Instance Variables
    3. Accessors
    4. Class Variables
    5. Class Methods
      1. Singletons
    6. Inheritance
    7. Modules
    8. public, private, or protected
    9. Review Questions
  13. 10. More Fun with Ruby
    1. Formatting Output with sprintf
    2. Processing XML
      1. REXML
      2. Builder
    3. Date and Time
      1. The Time Class
      2. The Date Class
        1. Calendar forms
    4. Reflection
      1. Reflection on Variables and Constants
      2. Reflection on Methods
    5. Using Tk
    6. Metaprogramming
    7. RubyGems
    8. Exception Handling
    9. Creating Documentation with RDoc
      1. RDoc Basics
      2. Processing Files with RDoc
    10. Embedded Ruby
    11. Review Questions
  14. 11. A Short Guide to Ruby on Rails
    1. Where Did Rails Come From
    2. Why Rails?
      1. A Full-Stack Framework
      2. Don’t Repeat Yourself
      3. Convention over Configuration
      4. I Want My MVC
      5. Scripts
      6. Validation
      7. Ajax
      8. Migrations
      9. Console
      10. Environments and Testing
      11. Capistrano
      12. Rake
    3. What Have Other Folks Done with Rails?
    4. Hosting Rails
    5. Installing Rails
      1.  
        1. Using RubyGems to install Rails
      2. Other Installation Information
    6. Learning Rails
      1. Ruby Tutorials and Books
    7. A Brief Tutorial
    8. Review Questions
  15. A. Ruby Reference
    1. Ruby Interpreter
    2. Ruby’s Reserved Words
    3. Operators
    4. Escape Characters
    5. Predefined Variables
    6. Global Constants
    7. Regular Expressions
    8. String Unpack Directives
    9. Array Pack Directives
    10. Sprintf Flags and Field Types
    11. File Tests
    12. Time Formatting Directives
    13. RDoc Options
    14. Rake
  16. B. Answers to Review Questions
    1. Chapter 1 Review Questions
    2. Chapter 2 Review Questions
    3. Chapter 3 Review Questions
    4. Chapter 4 Review Questions
    5. Chapter 5 Review Questions
    6. Chapter 6 Review Questions
    7. Chapter 7 Review Questions
    8. Chapter 8 Review Questions
    9. Chapter 9 Review Questions
    10. Chapter 10 Review Questions
    11. Chapter 11 Review Questions
  17. Glossary
  18. Index
  19. About the Author
  20. Colophon
  21. Copyright

Product information

  • Title: Learning Ruby
  • Author(s): Michael Fitzgerald
  • Release date: May 2007
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596529864