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