0%

Book Description

Want to learn the fundamentals of Ruby programming but aren't sure where to start? Look no further! Ruby is a free, easy-to-learn, yet powerful scripting programming language that can run on any operating system. These attributes have made Ruby an extremely popular language in recent years for almost any programming task. Ruby Programming for the Absolute Beginner teaches you the basics of computer programming with Ruby through the creation of simple computer games. Not only will this "learn by doing" approach provide you with an instant sense of accomplishment, but it's also a fun way to learn. In addition to learning Ruby, you'll also learn the basics of computer programming, so you'll have a solid foundation from which you can confidently jump to other programming languages.

Table of Contents

  1. Copyright
    1. Dedication
  2. Acknowledgments
  3. About the Author
  4. Introduction
    1. Why Ruby?
    2. Who Should Read This Book?
    3. What You Need to Begin
    4. How This Book Is Organized
    5. Conventions Used in This Book
  5. I. Introducing Ruby
    1. 1. Ruby Basics
      1. Project Preview: The Ruby Joke Game
      2. Introducing Ruby
        1. Ruby Is Simple Yet Powerful
          1. Ruby Is Interpreted
          2. Ruby Supports a Natural English-Like Programming Style
          3. Ruby Has Light Syntax Requirements
        2. Ruby Is Object Oriented
        3. Ruby Is Extremely Flexible
        4. Ruby Exists in Many Different Environments
      3. Getting Ready to Work with Ruby
        1. Determining Whether Ruby Is Already Installed
          1. Looking for Ruby on Microsoft Windows
          2. Looking for Ruby on Mac OS X
          3. Looking for Ruby on UNIX and Linux
        2. Installing or Upgrading Ruby
          1. Installing Ruby on Microsoft Windows
          2. Finding a Ruby Installation for Mac OS X
          3. Installing Ruby on UNIX and Linux
      4. Working with Ruby Interactively
        1. Working at the Command Prompt
        2. IRB—Interactive Ruby
        3. FXRI—Interactive Ruby Help and Console
      5. Developing Ruby Scripts
        1. Creating Ruby Scripts on Microsoft Windows
        2. Creating Ruby Scripts on Mac OS X
        3. Creating Ruby Scripts on Linux and UNIX
        4. Using a Cross-Platform Ruby Editor
        5. Creating Your First Ruby Script
        6. Running Your Ruby Script
      6. Back to the Ruby Joke Game
        1. Designing the Game
          1. Step 1: Creating a New Ruby File
          2. Step 2: Documenting the Script and Its Purpose
          3. Step 3: Defining a Class Representing the Computer Screen
          4. Step 4: Instantiating New Objects
          5. Step 5: Prompting the Player for Permission to Continue
          6. Step 6: Outlining the Script’s High-Level Conditional Logic
          7. Step 7: Clearing the Screen
          8. Step 8: Telling the First Joke
          9. Step 9: Telling the Remaining Jokes
          10. Step 10: Thanking the Player
        2. Running Your New Ruby Script Game
      7. Summary
    2. 2. Interacting with Ruby
      1. Project Preview: The Ruby Tall Tale Game
      2. Getting to Know the IRB
        1. Starting an irb Session
        2. Working with Multiple irb Sessions
      3. Accessing the irb Online
      4. Working with the irb
        1. Executing Ruby Statements
        2. Following Ruby’s Syntactical Rules
        3. Executing Multiline Statements
        4. Using irb to Test Ruby Scripts
      5. Using the IRB to Perform Mathematical Calculations
        1. Using the irb as a Calculator
        2. Accessing Methods Stored in the Math Module
        3. Operator Precedence
        4. Overriding Operator Precedence
        5. Integers Versus Floating-point Numbers
      6. Accessing Ruby Documentation
      7. Back to the Ruby Tall Tale Game
        1. Designing the Ruby Tall Tale Game
          1. Step 1: Creating a New Ruby File
          2. Step 2: Documenting the Script and Its Purpose
          3. Step 3: Defining Custom Classes
          4. Step 4: Preparing the Game for Execution
          5. Step 5: Outlining the Script’s High-Level Conditional Logic
          6. Step 6: Prematurely Terminating Game Execution
          7. Step 7: Collecting Player Input
          8. Step 8: Building the Game’s Story
          9. Step 9: Telling the Game’s Story
          10. Step 10: Thanking the Player
        2. Running Your New Ruby Script Game
      8. Summary
  6. II. Learning How to Write Ruby Scripts
    1. 3. Working with Objects, Strings, and Variables
      1. Project Preview: The Ruby Virtual Crazy 8 Ball Game
      2. Working with Text Strings
        1. Formatting Text Strings
        2. Variable Interpolation
      3. Other Options for Manipulating Strings
        1. Concatenating Text Strings
        2. Multiplying Text Strings
        3. Comparing Text Strings
        4. Creating Multiline Text Strings
      4. Working with String Class Methods
      5. Object-Oriented Programming
        1. Defining a New Class
        2. Defining Class Properties
        3. Instantiating and Interacting with New Objects
        4. Defining Class Methods
        5. Inheritance
      6. Converting from One Class to Another
        1. Implicit Class Conversion
        2. Explicit Class Conversion
      7. Storing and Retrieving Data
        1. Naming Variables
        2. Variable Assignments
        3. Variable Scope
      8. Storing Data That Does Not Change
      9. Back to the Ruby Virtual Crazy 8 Ball Game
        1. Designing the Game
          1. Step 1: Creating a New Ruby File
          2. Step 2: Documenting the Script and Its Purpose
          3. Step 3: Defining a Screen Class
          4. Step 4: Defining a Ball Class
          5. Step 5: Instantiating New Objects
          6. Step 6: Greeting the Player
          7. Step 7: Prompting for Confirmation to Continue
          8. Step 8: Analyzing the Player’s Response
          9. Step 9: Managing Early Termination of the Game
          10. Step 10: Responding to Player Questions
        2. Running Your New Ruby Script Game
      10. Summary
    2. 4. Implementing Conditional Logic
      1. Project Preview: The Ruby Typing Challenge Game
      2. Using Conditional Logic to Create Adaptive Scripts
      3. Performing Alternative Types of Comparisons
      4. Conditional Logic Modifiers
        1. The if Modifier
        2. The unless Modifier
      5. Working with if and unless Expressions
        1. Building if Expressions
        2. Replacing if Modifiers with if Expressions
          1. Creating Single-Line if Expressions
          2. Providing an Alternative Course of Action
          3. Checking for Alternative Conditions
        3. Creating unless Expressions
      6. Using Case Blocks to Analyze Data
      7. Using the Ternary Operator
      8. Nesting Conditional Statements
      9. Combining and Negating Logical Comparison Operations
      10. Back to the Ruby Typing Challenge Game
        1. Designing the Game
          1. Step 1: Creating a New Ruby File
          2. Step 2: Documenting the Script and Its Purpose
          3. Step 3: Defining a Class Representing the Console Window
          4. Step 4: Defining a Class Representing the Typing Test
          5. Step 5: Defining the display_greeting Method
          6. Step 6: Defining the display_instructions Method
          7. Step 7: Defining the present_test Method
          8. Step 8: Defining the determine_grade Method
          9. Step 9: Initializing Script Objects
          10. Step 10: Getting Permission to Begin the Test
          11. Step 11: Developing the Controlling Logic Required to Administer the Test
          12. Step 12: Managing Early Game Termination
          13. Step 13: Executing the Typing Test
        2. Running Your New Ruby Script Game
      11. Summary
    3. 5. Working with Loops
      1. Project Preview: The Superman Movie Trivia Quiz
      2. Getting Loopy
      3. Repeating Things Using Loops
        1. Working with while Loops
        2. Working with until Loops
        3. Working with for...in Loops
      4. Using Loop Modifiers
        1. The while Modifier
        2. The until Modifier
      5. Executing Looping Methods
        1. Working with the each Method
        2. Working with the times Method
        3. Working with the upto Method
        4. Working with the downto Method
        5. Working with the step Method
        6. Working with the loop Method
      6. Altering Loop Execution
        1. Prematurely Terminating Loop Execution
        2. Repeating the Current Execution of a Loop
        3. Skipping to the Next Iteration of a Loop
        4. Restarting a Loop from the Beginning
      7. Back to the Superman Movie Trivia Quiz
        1. Designing the Game
          1. Step 1: Creating a New Ruby File
          2. Step 2: Documenting the Script and Its Purpose
          3. Step 3: Defining a Screen Class
          4. Step 4: Defining a Class Representing the Quiz
          5. Step 5: Defining the display_greeting Method
          6. Step 6: Defining the display_instructions Method
          7. Step 7: Defining the disp_q Method
          8. Step 8: Defining the determine_grade Method
          9. Step 9: Defining the display_credits Method
          10. Step 10: Initializing Script Objects
          11. Step 11: Getting Permission to Start the Quiz
          12. Step 12: Administering the Quiz
        2. Running Your New Ruby Script Game
      8. Summary
    4. 6. Working with Collections of Data
      1. Project Preview: The Ruby Number Guessing Game
      2. Processing Related Data as a Unit
      3. Storing Lists Using Arrays
        1. Declaring Arrays
          1. Creating an Array of Strings
          2. Using the New Method to Create an Array
          3. Assigning the Content of One Array to Another
          4. Creating a New Array from Two Existing Arrays
        2. Adding and Modifying Array Items
          1. Modifying an Existing Array Item
          2. Adding a New Item to an Array
          3. Working with the << Method
          4. Working with the push Method
        3. Determining if an Array Is Empty
        4. Retrieving Items from an Array
          1. Retrieving Individual Array Elements
          2. Retrieving Individual Array Elements Using the at Method
          3. Retrieving a Slice
          4. Retrieving the First and Last Elements Stored in an Array
        5. Using a Loop to Process the Contents of an Array
        6. Deleting Elements from an Array
          1. Deleting All the Items Stored in an Array
          2. Deleting the First Item Stored in an Array
          3. Deleting the Last Item Stored in an Array
          4. Using the Array Class’s delete Method
          5. Deleting Individual Items Using the at Method
        7. Sorting the Contents of an Array
        8. Searching an Array
      4. Storing Data Using Hashes
        1. Creating a New Hash File
          1. Defining a New Hash File
          2. Spreading Out a Hash Definition over Multiple Lines
          3. Using the Hash Class’s New Method
        2. Adding and Deleting Key-Value Pairs
          1. Using One Hash to Populate Another Hash
          2. Using the Hash Class’s merge Method
        3. Deleting a Hash’s Key-Value Pairs
          1. Deleting Specific Key-Value Pairs
          2. Conditionally Deleting Key-Value Pairs
        4. Determining the Number of Key-Value Pairs in a Hash
        5. Accessing Data Stored in Hashes
          1. Looping Through a Hash’s Keys
          2. Looping Through a Hash’s Values
        6. Sorting Hash Keys
      5. Back to the Ruby Number Guessing Game
        1. Designing the Game
          1. Step 1: Creating a New Ruby File
          2. Step 2: Documenting the Script and Its Purpose
          3. Step 3: Creating the Screen Class
          4. Step 4: Creating the Game Class
          5. Step 5: Defining the display_greeting Method
          6. Step 6: Defining the display_instructions Method
          7. Step 7: Defining the generate_number Method
          8. Step 8: Defining the play_game Method
          9. Step 9: Defining the display_credits Method
          10. Step 10: Initializing Script Objects
          11. Step 11: Getting Permission to Start the Game
          12. Step 12: Controlling Game Play
        2. Running Your New Ruby Script Game
      6. Summary
  7. III. Advanced Topics
    1. 7. Working with Regular Expressions
      1. Project Preview: The Word Guessing Game
      2. The Basics of Working with Regular Expressions
      3. Learning How to Match Basic Patterns
        1. Matching Basic Patterns
        2. Matching Alternate Patterns
      4. Understanding How to Work with Metacharacters
        1. Matching Individual Characters
        2. Matching a Pattern at the Beginning of a String
        3. Matching a Pattern at the End of a String
        4. Matching One or Not at All
        5. Matching Zero or More Times
        6. Matching Any of a Collection of Characters
      5. Other Common Uses of Regular Expressions
        1. Overcoming Differences in Case
        2. String Substitution
      6. Back to the Word Guessing Game
        1. Designing the Game
          1. Step 1: Creating a New Ruby File
          2. Step 2: Documenting the Script and Its Purpose
          3. Step 3: Creating the Screen Class
          4. Step 4: Creating the Game Class
          5. Step 5: Defining the display_greeting Method
          6. Step 6: Defining the display_instructions Method
          7. Step 7: Defining the select_word Method
          8. Step 8: Defining the get_consonants Method
          9. Step 9: Defining the get_vowel Method
          10. Step 10: Defining the prompt_for_guess Method
          11. Step 11: Defining the play_game Method
          12. Step 12: Defining the display_credits Method
          13. Step 13: Initializing Script Objects
          14. Step 14: Getting Permission to Start the Game
          15. Step 15: Controlling Game Play
        2. Running Your New Ruby Script Game
      7. Summary
    2. 8. Object-Oriented Programming
      1. Project Preview: The Ruby Rock, Paper, Scissors Game
      2. Understanding Key Object-Oriented Terms
        1. Abstraction
        2. Encapsulation
        3. Inheritance
        4. Polymorphism
      3. Initializing Objects upon Instantiation
      4. Understanding Variable Scope
        1. Working with Local Variables
        2. Working with Global Variables
        3. Working with Instance Variables
        4. Working with Class Variables
      5. Taking Advantage of Ruby’s Built-in Classes
      6. Modifying Ruby Classes
      7. Back to the Ruby Rock, Paper, Scissors Game
        1. Designing the Game
          1. Step 1: Creating a New Ruby File
          2. Step 2: Documenting the Script and Its Purpose
          3. Step 3: Defining a Screen Class
          4. Step 4: Creating the Game Class
          5. Step 5: Defining the display_greeting Method
          6. Step 6: Defining the display_instructions Method
          7. Step 7: Defining the play_game Method
          8. Step 8: Defining the get_player_move Method
          9. Step 9: Defining the get_computer_move Method
          10. Step 10: Defining the analyze_results Method
          11. Step 11: Defining the display_results Method
          12. Step 12: Defining the display_credits Method
          13. Step 13: Initializing Script Objects
          14. Step 14: Getting Permission to Start the Game
          15. Step 15: Controlling Game Play
        2. Running Your New Ruby Script Game
      8. Summary
    3. 9. File and Folder Administration
      1. Project Preview: The Ruby Blackjack Game
      2. Understanding File Input and Output
      3. Administering Text Files and Folders
        1. Verifying Whether a File or Folder Exists
        2. Retrieving Information about a File or Folder
          1. Determining if a Resource Is a File or a Folder
          2. Checking a File’s Size
          3. Examining Folder Contents
        3. Creating New Folders
        4. Deleting Files and Folders
        5. Renaming Files
      4. Working with Files and Folders on Different Operating Systems
      5. Reading from and Writing to Text Files
        1. Writing Data to Text Files
        2. Appending Data to the Ends of Text Files
        3. Reading Data from Text Files
      6. Back to the Ruby Blackjack Game
        1. Designing the Game
          1. Step 1: Creating a New Ruby File
          2. Step 2: Documenting the Script and Its Purpose
          3. Step 3: Defining a Screen Class
          4. Step 4: Creating the Game Class
          5. Step 5: Defining the display_greeting Method
          6. Step 6: Defining the display_instructions Method
          7. Step 7: Defining the play_game Method
          8. Step 8: Defining the get_new_card Method
          9. Step 9: Defining the complete_player_hand Method
          10. Step 10: Defining the play_dealer_hand Method
          11. Step 11: Defining the determine_winner Method
          12. Step 12: Defining the display_credits Method
          13. Step 13: Initializing Script Objects
          14. Step 14: Getting Permission to Start the Game
          15. Step 15: Controlling Game Play
        2. Running Your New Ruby Script Game
      7. Summary
    4. 10. Debugging
      1. Project Preview: The Ruby Tic-Tac-Toe Game
      2. Analyzing Different Types of Script Errors
        1. Watching Out for Syntax Errors
        2. Preventing Runtime Errors
        3. Looking Out for Logical Errors
      3. Handling Exceptions
        1. Creating Exception Handlers
        2. Accessing Error Information
        3. Handling Different Types of Errors
      4. Tracking the Logical Flow of Your Scripts
      5. Finding Bugs Using the Ruby Debugger
        1. Starting the Debugger
      6. Back to the Ruby Tic-Tac-Toe Game
        1. Designing the Game
          1. Step 1: Creating a New Ruby File
          2. Step 2: Documenting the Script and Its Purpose
          3. Step 3: Defining a Screen Class
          4. Step 4: Creating the Game Class
          5. Step 5: Defining the display_greeting Method
          6. Step 6: Defining the display_instructions Method
          7. Step 7: Defining the display_game_board Method
          8. Step 8: Defining the validate_player_move Method
          9. Step 9: Defining the play_game Method
          10. Step 10: Defining the clear_game_board Method
          11. Step 11: Defining the check_results Method
          12. Step 12: Defining the display_game_results Method
          13. Step 13: Defining the display_credits Method
          14. Step 14: Initializing Script Objects
          15. Step 15: Getting Permission to Start the Game
          16. Step 16: Controlling Game Play
        2. Running Your New Ruby Script Game
      7. Summary
  8. IV. Appendices
    1. A. What’s on the Companion Website?
    2. B. What Next?
      1. Recommended Reading
      2. Ruby Web Pages
        1. Ruby Programming Language
        2. RubyForge
        3. Ruby-doc.org
        4. Ruby on Rails
      3. Ruby Mailing Lists
        1. ruby-talk
        2. ruby-doc
      4. Ruby Newsgroups
        1. comp.lang.ruby
        2. Rails Weenie
      5. Ruby Blogs
        1. Ruby Inside
        2. Ruby Underground
  9. Glossary
18.226.169.94