Difference between revisions of "Ruby"

From ArchWiki
Jump to: navigation, search
(Shortened the title of several section titles. (no inter-wiki links to the titles appear to exist) Tweaked sample Ruby version numbers.)
m (Grammar fix.)
Line 3: Line 3:
  
 
== Installing Ruby ==
 
== Installing Ruby ==
The version of Ruby you need to install depends on your requirements. If you are supporting a legacy application, install Ruby 1.9 or 1.8 as necessary. If you are starting a new project, Ruby 2.0 is recommended. Below is a summary of the available versions and how to get them:
+
The version of Ruby you need to install depends on your requirements. If you are supporting a legacy application, install Ruby 1.9 or 1.8 as necessary. If you are starting a new project, Ruby 2.0 is recommended. Below is a summary of the available versions and how to get them.
  
 
=== Ruby 2.0 ===
 
=== Ruby 2.0 ===

Revision as of 00:11, 31 March 2013

Ruby is a dynamic, interpreted, open source programming language with a focus on simplicity and productivity.

Installing Ruby

The version of Ruby you need to install depends on your requirements. If you are supporting a legacy application, install Ruby 1.9 or 1.8 as necessary. If you are starting a new project, Ruby 2.0 is recommended. Below is a summary of the available versions and how to get them.

Ruby 2.0

To install Ruby 2.0.0, install ruby. Ruby 2.0 includes #RubyGems.

Ruby 1.9

To install Ruby 1.9.3, install ruby1.9. Ruby 1.9 includes RubyGems.

Pros:

  • Vastly improved performance over 1.8
  • New features for concurrency such as fibers.
  • Various other language improvements, such as an improved CSV parser.

Cons:

  • Not compatible with many older gems (and Ruby On Rails versions prior to 2.3)
  • Changes in the language might cause older Ruby code not to run, or exhibit unexpected bugs.
Note: Visit http://isitruby19.com/ to determine if the gems/modules you require are compatible with Ruby 1.9.

Ruby 1.8

To install Ruby 1.8.7, install ruby1.8AUR or ruby-1.8.7-svnAUR from the AUR. RubyGems is not included with Ruby 1.8. Instead, it is available through the rubygems1.8AUR package.

Multiple Versions

If you want to run multiple versions on the same system (e.g. 2.0.0-p0 and 1.9.3-p392), the easiest way is to use RVM or rbenv.

RubyGems

gem is the package manager of sorts for Ruby modules (called Gems), somewhat comparable to what pacman is to Arch Linux. The gem command will be installed if you followed the installation instructions above.

Running as normal user

When running gem as a user, the gems will be installed into ~/.gem and not affect anyone else, although it might be worth noting that not all gems are happy with being installed in this way, and might insist on being installed by root (especially if they have native extensions). This is considered the best way to manage gems on Arch.

To use gems which install binaries, you need to add ~/.gem/ruby/2.0.0/bin to your $PATH.

This per-user behavior is enabled via /etc/gemrc and can be overridden by a ~/.gemrc file.

Running as root

When running as root, the gems will be installed into /root/.gems and will not be installed to /usr/lib/ruby/gems/.

Note: See bug #33327 for more information.

Bundler solves these problems to some extent by packaging gems into your application. See the section below on using bundler.

Updating RubyGems

$ gem update

Installing a gem

This example installs the MySQL ruby gem:

$ gem install mysql

The process can be sped up somewhat if you do not need local documentation:

$ gem install mysql --no-rdoc --no-ri

The gem will now be downloaded, compiled if necessary, and installed.

Bundler

Bundler allows you to specify which gems your application depends upon, and optionally which version those gems should be. Once this specification is in place, Bundler installs all required gems (including the full gem dependency tree) and logs the results for later inspection. By default, Bundler installs gems into a shared location, but they can also be installed directly into your application. When your application is run, Bundler provides the correct version of each gem, even if multiple versions of each gem have been installed. This requires a little bit of work: applications should be called with bundle exec, and two lines of boilerplate code must be placed in your application's main executable.

To install Bundler:

$ gem install bundler

By default, Bundler installs gems system-wide, which is contrary to the behaviour of gem itself on Arch. To correct this, add the following to your ~/.bashrc:

export GEM_HOME=~/.gem/ruby/2.0.0

Curiously, this means that all gems are placed in the 2.0.0 directory, even those gems that are not compatible with Ruby 2.0.

To start a new bundle:

$ bundle init

Then edit Gemfile in the current directory (created by bundle init) and list your required gems:

Gemfile
gem "rails", "3.2.9"
gem "mysql"

Run the following to install gems into GEM_HOME:

$ bundle install

Alternatively, run the following to install gems to .bundle in the working directory:

$ bundle install --path .bundle

Don't forget to edit your main executable:

#!/usr/bin/env ruby

# "This will automatically discover your Gemfile, and make all of the gems in
# your Gemfile available to Ruby." http://gembundler.com/v1.3/rationale.html
require 'rubygems'
require 'bundler/setup'

...

Finally, run your program:

bundle exec <main_executable_name.rb>

Managing RubyGems using pacman

Instead of using the gem command directly you can use pacman to manage the installed gems like normal packages. There are a lot of ruby packages available from AUR. Ruby packages follow the naming convention ruby-[gemname]. As an alternative you can use the tool pacgemAUR which automatically creates arch packages from gems and installs them afterwards using pacman.

Warning: Many ruby gem packages in the AUR explicitly use the --no-user-install or --user-install command line switches, bypassing the global setting found in /etc/gemrc or the users own ~/.gemrc. You're editing the PKGBUILD file before you install, right?

See also

References