Product Description
Ruby is an increasingly popular, fully object-oriented dynamic programming language, hailed by many practitioners as the finest and most useful language available today. When Ruby first burst onto the scene in the Western world, the Pragmatic Programmers were there with the definitive reference manual, Programming Ruby: The Pragmatic Programmer's Guide.
Now in its second edition, author Dave Thomas has expanded the famous Pickaxe book with over 200 pages of new content, covering all the improved language features of Ruby 1.8 and standard library modules. The Pickaxe contains four major sections:
- An acclaimed tutorial on using Ruby.
- The definitive reference to the language.
- Complete documentation on all built-in classes, modules, and methods
- Complete descriptions of all 98 standard libraries.
If you enjoyed the First Edition, you'll appreciate the expanded content, including enhanced coverage of installation, packaging, documenting Ruby source code, threading and synchronization, and enhancing Ruby's capabilities using C-language extensions. Programming for the World Wide Web is easy in Ruby, with new chapters on XML/RPC, SOAP, distributed Ruby, templating systems, and other web services. There's even a new chapter on unit testing.
This is the definitive reference manual for Ruby, including a description of all the standard library modules, a complete reference to all built-in classes and modules (including more than 250 significant changes since the First Edition). Coverage of other features has grown tremendously, including details on how to harness the sophisticated capabilities of irb, so you can dynamically examine and experiment with your running code. "Ruby is a wonderfully powerful and useful language, and whenever I'm working with it this book is at my side" --Martin Fowler, Chief Scientist, ThoughtWorks
From the Publisher
About the Author
Excerpted from Programming Ruby: The Pragmatic Programmer's Guide by Dave Thomas, Chad Fowler, Andy Hunt. Copyright © 2004. Reprinted by permission. All rights reserved.
Chad Fowler is a leading figure in the Ruby community. Hes on the board of Ruby Central, Inc. Hes one of the organizers of RubyConf. And hes one of the writers of RubyGems. All this makes him uniquely qualified to write this chapter.
RubyGems is a standardized packaging and installation framework for libraries and applications, making it easy to locate, install, upgrade, and uninstall Ruby packages. It provides users and developers with four main facilities.
1. A standardized package format,
2. A central repository for hosting packages in this format,
3. Installation and management of multiple, simultaneously installed versions of the same library,
4. End-user tools for querying, installing, uninstalling, and otherwise manipulating these packages.
Before RubyGems came along, installing a new library involved searching the Web, downloading a package, and attempting to install itonly to find that its dependencies havent been met. If the library you want is packaged using RubyGems, however, you can now simply ask RubyGems to install it (and all its dependencies). Everything is done for you.
In the RubyGems world, developers bundle their applications and libraries into single files called gems. These files conform to a standardized format, and the RubyGems system provides a command-line tool, appropriately named gem, formanipulating these gem files.
In this chapter, well see how to
1. Install RubyGems on your computer.
2. Use RubyGems to install other applications and libraries.
3. Write your own gems.After downloading
and unpacking the distribution, you can install it using the included installation script.
% cd rubygems0.7.0
% ruby install.rb
Depending on your operating system, you may need suitable privileges to write files into Rubys site_ruby/ and bin/ directories.
The best way to test that RubyGems was installed successfully also happens to be the most important command youll learn.
% gem help
RubyGems is a sophisticated package manager for Ruby. This is a basic help message containing pointers to more information.
Usage:
gem h/
help
gem v/
version
gem command [arguments...] [options...]
Examples:
gem install rake
gem list local
gem build package.gemspec
gem help install
Further help:
gem help commands list all 'gem' commands
gem help examples show some examples of usage
gem help show help on COMMAND
(e.g.
Because RubyGems help is quite comprehensive, we wont go into detail about each of the available RubyGems commands and options in this chapter.
Installing Application Gems
Lets start by using RubyGems to install an application that is written in Ruby.Not only that, but its generally a great tool to have around, as it is a build tool similar to Make and Ant. In fact, you can even use Rake to build gems!
Locating and installing Rake with RubyGems is simple.
% gem install r
rake
Attempting remote installation of 'Rake'
Successfully installed rake, version 0.4.3
% rake version
rake, version 0.4.3
RubyGems downloads the Rake package and installs it. Because Rake is an application, RubyGems downloads both the Rake libraries and the command-line program rake.
You control the gem program using subcommands, each of which has its own options and help screen. In this example, we used the install subcommand with the -r option, which tells it to operate remotely. (Many RubyGems operations can be performed either locally or remotely. For example, you can to use the query command either to display all the gems that are available remotely for installation or to display a list of gems you
already have installed. For this reason, subcommands accept the options r and-l, specifying whether an operation is meant to be carried out remotely or locally.)
If for some reasonperhaps because of a potential compatibility issueyou wanted an older version of Rake, you could use RubyGems version requirement operators to specify criteria by which a version would be selected.
% gem install r
rake v
"< 0.4.3"
Attempting remote installation of 'rake'
Successfully installed rake, version 0.4.2
% rake version
rake, version 0.4.2
Table 17.1 on the following page lists the version requirement operators. The -v argument in our previous example asks for the highest version lower than 0.4.3.
Theres a subtlety when it comes to installing different versions of the same application with RubyGems. Even though RubyGems keeps separate versions of the applications library files, it does not version the actual command you use to run the application. As a result, each install of an application effectively overwrites the previous one.
During installation, you can also add the -t option to the RubyGems install command, causing RubyGems to run the gems test suite (if one has been created). If the tests fail, the installer will prompt you to either keep or discard the gem. This is a good way to gain a little more confidence that the gem youve just downloaded works on your system the way the author intended.
% gem install SomePoorlyTestedProgram t
Attempting local installation of 'SomePoorlyTestedProgram1.0.1'
Successfully installed SomePoorlyTestedProgram, version 1.0.1
23 tests, 22 assertions, 0 failures, 1 errors...keep Gem? [Y/n] n
Successfully uninstalled SomePoorlyTestedProgram version 1.0.1
Had we chosen the default and kept the gem installed, we could have inspected the gem to try to determine the cause of the failing test.
Both the require_gem method and the add_dependency attribute in a Gem::Specification accept an argument that specifies a version dependency. RubyGems version dependencies are of the form operator major.minor.patch_level. Listed below is a table of all the possible version operators.