You are previewing Learning Ruby.

Learning Ruby

Cover of Learning Ruby by Michael Fitzgerald Published by O'Reilly Media, Inc.
  1. Learning Ruby
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    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
      2. Interactive Ruby
      3. Resources
      4. Installing Ruby
      5. Permission Denied
      6. Associating File Types on Windows
      7. Review Questions
    5. 2. A Quick Tour of Ruby
      1. Ruby Is Object-Oriented
      2. Ruby's Reserved Words
      3. Comments
      4. Variables
      5. Strings
      6. Numbers and Operators
      7. Conditional Statements
      8. Arrays and Hashes
      9. Methods
      10. Blocks
      11. Symbols
      12. Exception Handling
      13. Ruby Documentation
      14. Review Questions
    6. 3. Conditional Love
      1. The if Statement
      2. The case Statement
      3. The while Loop
      4. The loop Method
      5. The for loop
      6. Execution Before or After a Program
      7. Review Questions
    7. 4. Strings
      1. Creating Strings
      2. Concatenating Strings
      3. Accessing Strings
      4. Comparing Strings
      5. Manipulating Strings
      6. Case Conversion
      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
      4. Ranges
      5. Inquiring About Numbers
      6. More Math Methods
      7. Math Functions
      8. Rational Numbers
      9. Prime Numbers
      10. Review Questions
    9. 6. Arrays
      1. Creating Arrays
      2. Accessing Elements
      3. Concatenation
      4. Set Operations
      5. Unique Elements
      6. Blow Your Stack
      7. Comparing Arrays
      8. Changing Elements
      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
      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
      2. Creating a New File
      3. Opening an Existing File
      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
      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
      3. Date and Time
      4. Reflection
      5. Using Tk
      6. Metaprogramming
      7. RubyGems
      8. Exception Handling
      9. Creating Documentation 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?
      3. What Have Other Folks Done with Rails?
      4. Hosting Rails
      5. Installing Rails
      6. Learning Rails
      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. SPECIAL OFFER: Upgrade this ebook with O’Reilly
O'Reilly logo

Installing Ruby

Ruby is available on the major platforms. The following sections show you how to install Ruby on Mac OS X, Windows, and Linux. Ruby's general download page is at http://www.ruby-lang.org/en/downloads. Most of you could likely figure out how to install Ruby just by following the links there, but the material here provides a little extra guidance.

Installation procedures are a moving target, and print media can't keep up with electronic media. That means that some of this material may get out of sync with what's happening out there on the Web, so I'll be as generally specific as I can.

Installing Ruby on Mac OS X Tiger

As shipped, Tiger comes with an older version of Ruby. Which version depends on what release of Tiger you're dealing with. The release of Tiger on my system at the moment is 10.4.8, which comes with version 1.8.2. You'll want an updated version, as I did.

The simple way to install Ruby (and a boatload of other software) is with Locomotive (http://locomotive.raaum.org). For information on what comes with the Locomotive download (a dmg file), which includes Ruby on Rails, see http://locomotive.raaum.org/bundles.html. It might be more than you want to deal with. You can find a mirror at http://prdownloads.sourceforge.net/locomotive/Locomotive_2.0.8.dmg?download. Select a mirror and then follow the steps just like you would when installing any other dmg.

The purest form of installation, at least in my mind, is to download and compile the source files. In other words, you download the file distribution for a given release, pull the files out of the release archive, compile the files (those that need compilation), and then copy those files to their proper directories. Those are the basic steps, but there are a few tools to make this job easier, like configure and make. We'll take advantage of them here as we install a new version of Ruby on Tiger (these steps could apply to a Linux installation as well).

These steps may appear daunting at first, but they really are not. Just follow along and things will come together in the end.

You can find excellent instructions on installing Ruby on Tiger in Dan Benjamin's "Building Ruby, Rails, LightTPD, and MySQL on Tiger" (http://hivelogic.com/articles/2005/12/01/ruby_rails_lighttpd_mysql_tiger). He covers installing more software than you need to install now; I'll only use his steps for installing Ruby, and I'll update those steps to include the latest versions of software.

You need to have XCode installed on your Mac for this install procedure to work. XCode is a set of programming tools from Apple. You can learn about it at http://www.apple.com/macosx/features/xcode and download it from http://developer.apple.com/tools/download. The download instructions are easy to follow.

As shipped, Tiger has some issues with Ruby (see http://wiki.rubyonrails.com/rails/pages/HowtoInstallOnOSXTiger). One way to resolve some of the problems is by downloading and installing readline (http://tiswww.tis.case.edu/~chet/readline/readline.html), which lets you do command-line editing (irb uses readline). Here are the steps for downloading and installing readline:

  1. Go to ftp://ftp.gnu.org/gnu/readline to find the latest version (5.2 at this writing) and download it. (I put source archives in the directory /usr/local/src on my Mac so I can keep track of them.) You can avoid using the browser or FTP. Just use curl (http://curl.haxx.se). The -O option takes the last part of the URL to create a desination filename.

    $ curl -O ftp://ftp.gnu.org/gnu/readline/readline-5.2.tar.gz
  2. Extract the archive with tar (x means extract, z means gunzip, v means verbose, f means use file archive):

    $ tar xzvf readline-5.2.tar.gz
  3. Change directories:

    $ cd readline-5.2
  4. Run configure (generated from Autoconf, a tool that produces shell scripts for configuring software packages), replacing {$prefix} with /usr/local:

    $ ./configure --prefix=/usr/local
  5. Run make, a tool for building applications. This compiles the source files, and gets things ready to install. You can test the results, too:

    $ make
    $ make test
  6. Finally, install:

    $ make install

    If you have not logged in as root, you can assume superuser powers by prefixing this command with the sudo utility (http://www.sudo.ws), which will require a password:

    $ sudo make install

The steps to install Ruby are very similar:

  1. While in /usr/local/src, grab the archive for the latest version of Ruby (1.8.6 at this writing):

    $ curl -O ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.6.tar.gz
  2. Extract the archive:

    $ tar xzvf ruby-1.8.6.tar.gz
  3. Change directories:

    $ cd ruby-1.8.6
  4. Run configure (enabling POSIX threads, with readline):

    $ ./configure --prefix=/usr/local --enable-pthread --with-readline-dir=/usr/local
  5. Run make and then test it:

    $ make
    $ make test
  6. Install the software:

    $ make install

    You may need the sudo utility (http://www.sudo.ws/), which will require a password):

    $ sudo make install
  7. Then install the documentation:

    $ make install-doc

    or:

    $ sudo make install-doc
  8. Place /usr/local/bin in the path if it is not already. If you don't know how to do this, see the sidebar "Setting Up the Path Environment," later in this chapter.

  9. Now test to make sure Ruby is in place:

    $ ruby -v
  10. You should get this happy reply:

    $ ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-darwin8.9.0]

Alrighty then. You are ready to roll with Ruby on Mac OS X.

Installing Ruby on Windows with the One-Click Installer

It's easy to install Ruby on Windows with the One-Click Installer, available on RubyForge at http://rubyforge.org/projects/rubyinstaller. Here are the steps:

  1. Go to the Ruby download site and click on the link labeled "1.8.6 One-Click Installer (or later)," or go to the One-Click Installer site and click the Download link. Click on the latest executable, which is ruby186-25.exe at this writing.

  2. Open the executable. An install wizard will appear (see Figure 1-5). You'll have a chance to include other goodies in the download, such as the SciTE editor (http://www.scintilla.org/SciTE.html). Also, be sure to enable RubyGems when asked, as it is installed by default, and you'll no doubt want use it later.

  3. Select a destination folder (such as C:\Ruby or C:\"Program Files"\Ruby). If you try to install over an older version of Ruby, you'll be asked to uninstall the old version first.

  4. Add the new Ruby bin directory to your path; for example, if your Ruby directory is C:\Ruby, add C:\Ruby\bin to your path (see the sidebar "Setting Up the Path Environment," later in this chapter, if you don't know how to do this; it's OK to set up the path after the installation).

  5. After you install Ruby, open a DOS window and type this line:

    $ ruby -v

    You should get something like the following response:

    $ ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
  6. Check your system path variable and make sure it contains the path to the Ruby binaries in the bin directory. The One-click installer should take care of all this for you, however.

Windows One-Click Installer

Figure 1-5. Windows One-Click Installer

Installing Ruby on Windows with Binaries

Installing Ruby using binaries (precompiled executables) is just as easy as using the One-Click Installer. I think it is, anyway. Here are the steps I suggest:

  1. Decide where you want to install the Ruby files—for example, C:\Ruby or C:\"Program Files"\Ruby.

  2. Download the stable binary ZIP archive for the latest release of Ruby (1.8.6 at this writing). Go to the Ruby download page at http://www.ruby-lang.org/en/downloads, and find the "Ruby on Windows" section, then click the link Ruby 1.8.6 Binary. Or you can just point to ftp://ftp.ruby-lang.org/pub/ruby/binaries/mswin32/ruby-1.8.5-i386-mswin32.zip (or latest version) in a browser. This will download the file archive.

  3. Open the archive (ruby-1.8.6-i386-mswin32.zip or later) with Windows Explorer, and then extract it to the directory you set up in step 1 (see Figure 1-6).

  4. Place the new Ruby bin directory in your path; for example, if your Ruby directory is C:\Ruby, add C:\Ruby\bin to your path (see the sidebar "Setting Up the Path Environment," later in this chapter, if you don't know how to do this).

  5. After you install Ruby, open a DOS window and type:

    $ ruby -v
  6. If you don't get something like the following answer, check your system path variable and make sure it contains the path to the Ruby binaries in the bin directory:

    $ ruby 1.8.6 (2006-08-25) [i386-mswin32]
    C:\Ruby\bin in Windows Explorer

    Figure 1-6. C:\Ruby\bin in Windows Explorer

Installing Ruby on Linux

The installation steps I discussed for installing Ruby on Mac OS X from source will also work for Linux, but I will just mention a few other options here. If you know Linux, you'll know what I am talking about.

The best content for your career. Discover unlimited learning on demand for around $1/day.