Difference between revisions of "Ruby"

From ArchWiki
Jump to: navigation, search
(Removed the one-liner for generating GEM_HOME, as it did not work correctly. Updated the suggested install directory from ~/.gem/ruby/1.9.3 to ~/.gem/ruby/2.0.0)
(1.9 is no longer in the official repo's)
(17 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
[[Category:Programming language]]
 
[[Category:Programming language]]
 +
[[ja:Ruby]]
 +
[[zh-CN:Ruby]]
 
Ruby is a dynamic, interpreted, open source programming language with a focus on simplicity and productivity.
 
Ruby is a dynamic, interpreted, open source programming language with a focus on simplicity and productivity.
  
 
== Installing Ruby ==
 
== Installing Ruby ==
The version of Ruby you need to install depends on your requirements, as not all 3rd party code is compatible with all versions. Here is a summary of the versions below and how to get them:
 
  
=== Ruby 1.9.3 (Stable) ===
+
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.
{{Note|This article is out of date: The current stable release in ArchLinux is Ruby 2.0.0.}}
+
'''Summary:''' Ruby 1.9 is recommended usage for new projects.
+
  
Pros:
+
=== Ruby 2.0 ===
* Vastly improved performance over 1.8
+
* New features for concurrency such as fibers.
+
* Various other language improvements, such as an improved CSV parser.
+
  
Cons:
+
To install Ruby 2.0.0, install {{Pkg|ruby}}. Ruby 2.0 includes [[#RubyGems|RubyGems]].
* 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.9 ===
  
To install Ruby 1.9, simply install {{Pkg|ruby}}.
+
To install Ruby 1.9, install {{AUR|ruby1.9}} from the [[AUR]]. Ruby 1.9 includes RubyGems.
  
Ruby 1.9 also includes RubyGems (detailed below), so you can easily update to the latest RubyGems using:
+
=== Ruby 1.8 ===
# gem update --system
+
  
=== Ruby 1.8.7 (Deprecated) ===
+
To install Ruby 1.8, install {{AUR|ruby1.8}} from the [[AUR]]. Ruby 1.8 does not include RubyGems. Instead, it is available through the {{AUR|rubygems1.8}} package.
'''Summary:''' Use Ruby 1.8.7 with any incompatible or out of date code as necessary.
+
  
Last stable version of 1.8, which is incompatible with 1.9. However, there is still code that is based on it.
+
=== Multiple versions ===
  
You can install {{AUR|ruby-1.8.7-svn}} or {{AUR|ruby1.8}} from the [[AUR]].
+
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 is not included with the {{AUR|ruby1.8}} package, so install {{AUR|rubygems1.8}} from the [[AUR]].
+
=== Documentation ===
  
=== Multiple versions ===
+
To make documentation available through the included {{ic|ri}} command-line tool, install {{Pkg|ruby-docs}}.
If you want to run multiple versions on the same system (e.g. '''1.9.3''' and '''1.8.7'''), the easiest way is to use [[RVM]] or [[rbenv]].
+
You can then query the docs with: {{ic|ri Array}}, {{ic|ri Array.pop}} etc. (much like man-pages)
  
 
== RubyGems ==
 
== 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.
+
 
 +
''gem'' is a package manager 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.
 +
 
 +
=== Usage ===
 +
 
 +
To see what gems are installed:
 +
$ gem list
 +
 
 +
To get information about a gem:
 +
$ gem spec ''gem_name''
 +
 
 +
By default, {{ic|gem list}} and {{ic|gem spec}} use the {{ic|--local}} option, which forces ''gem'' to search only the local system. This can be overridden with the {{ic|--remote}} flag. Thus, to search for the mysql gem:
 +
$ gem list --remote mysql
 +
 
 +
To install a 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
 +
 
 +
To update all installed gems:
 +
$ gem update
  
 
=== Running as normal user ===
 
=== Running as normal user ===
When running ''gem'' as a user, the gems will be installed into {{ic|~/.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 {{ic|~/.gem/ruby/1.9.3/bin}} to your {{ic|$PATH}}.
+
When running ''gem'' as a normal user, gems are installed into {{ic|~/.gem}} instead of system-wide. This is considered the best way to manage gems on Arch. Unfortunately, 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 (compiled C code). This per-user behavior is enabled via {{ic|/etc/gemrc}} and can be overridden by a {{ic|~/.gemrc}} file.
  
This per-user behavior is enabled via {{ic|/etc/gemrc}} and can be overridden by a {{ic|~/.gemrc}} file.
+
To use gems which install binaries, you need to add {{ic|~/.gem/ruby/2.0.0/bin}} to your {{ic|$PATH}}.
  
 
=== Running as root ===
 
=== Running as root ===
 +
 
When running as root, the gems will be installed into {{ic|/root/.gems}} and will '''not''' be installed to {{ic|/usr/lib/ruby/gems/}}.
 
When running as root, the gems will be installed into {{ic|/root/.gems}} and will '''not''' be installed to {{ic|/usr/lib/ruby/gems/}}.
  
Line 54: Line 67:
 
[[Ruby#Bundler|Bundler]] solves these problems to some extent by packaging gems into your application. See the section below on using bundler.
 
[[Ruby#Bundler|Bundler]] solves these problems to some extent by packaging gems into your application. See the section below on using bundler.
  
=== Updating RubyGems ===
+
=== Bundler ===
$ gem update
+
  
=== Installing a gem ===
+
[http://github.com/carlhuda/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 {{ic|bundle exec}}, and two lines of boilerplate code must be placed in your application's main executable.
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.
+
To install Bundler:
 
+
=== Bundler ===
+
[http://github.com/carlhuda/bundler Bundler] installs gems (including those with native extensions) directly into your application, which works very well for shared hosting and easy deployment of [[Ruby on Rails]] applications for example. Bundler also resolves dependencies as a whole, rather than individually like RubyGems, making things a lot easier.  To install:
+
 
  $ gem install bundler
 
  $ gem install bundler
  
Bundler seems to want to install gems system-wide, contrary to the current default behaviour of ''gem'' itself on Arch. To correct this, add the following line to your {{ic|~/.bashrc}}:
+
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 {{ic|~/.bashrc}}:
 
  export GEM_HOME=~/.gem/ruby/2.0.0
 
  export GEM_HOME=~/.gem/ruby/2.0.0
  
Line 76: Line 80:
 
  $ bundle init
 
  $ bundle init
  
Then add your required gems into "Gemfile" in the current directory (created by bundle init):
+
Then edit {{ic|Gemfile}} in the current directory (created by bundle init) and list your required gems:
 
+
 
{{hc|Gemfile|
 
{{hc|Gemfile|
 
gem "rails", "3.2.9"
 
gem "rails", "3.2.9"
Line 83: Line 86:
 
}}
 
}}
  
Finally, run the following to install your gems:
+
Run the following to install gems into {{ic|GEM_HOME}}:
 
  $ bundle install
 
  $ bundle install
  
Or, alternatively, in order to install locally to {{ic|.bundle}} under the working directory:
+
Alternatively, run the following to install gems to {{ic|.bundle}} in the working directory:
 
  $ bundle install --path .bundle
 
  $ bundle install --path .bundle
  
== Managing RubyGems using pacman ==
+
Don't forget to edit your main executable:
 +
{{bc|#!/usr/bin/env ruby
  
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 {{AUR|pacgem}} which automatically creates arch packages from gems and installs them afterwards using pacman.
+
# "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'
  
{{Warning|Many ruby gem packages in the AUR explicitly use the {{ic|--no-user-install}} or {{ic|--user-install}} command line switches, bypassing the global setting found in {{ic|/etc/gemrc}} or the users own {{ic|~/.gemrc}}. You're editing the PKGBUILD file before you install, right?}}
+
...
 +
}}
 +
 
 +
Finally, run your program:
 +
bundle exec ''main_executable_name.rb''
 +
 
 +
=== Managing RubyGems using pacman ===
 +
 
 +
Instead of managing gems with {{ic|gem}}, you can use {{ic|pacman}}, or some [[AUR]] helper. Ruby packages follow the naming convention ruby-[gemname]. This option provides the following advantages:
 +
 
 +
* Gems are updated along with the rest of your system. As a result, you never need to run {{ic|gem update}}: {{ic|# pacman -Syu}} suffices.
 +
* Installed gems are available system-wide, instead of being available only to the user who installed them.
 +
 
 +
If a gem is not available in the repositories, you can use {{AUR|pacgem}} to automatically create a package, which can then be installed by pacman.
  
 
== See also ==
 
== See also ==
* [[Ruby On Rails]]
 
  
== References ==
+
* [[Ruby On Rails]]
 
* Ruby - http://ruby-lang.org/
 
* Ruby - http://ruby-lang.org/
 
* Rubyforge - http://rubyforge.org
 
* Rubyforge - http://rubyforge.org
 
* Bundler - http://github.com/carlhuda/bundler
 
* Bundler - http://github.com/carlhuda/bundler

Revision as of 15:54, 1 July 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, install ruby1.9AUR from the AUR. Ruby 1.9 includes RubyGems.

Ruby 1.8

To install Ruby 1.8, install ruby1.8AUR from the AUR. Ruby 1.8 does not include RubyGems. 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.

Documentation

To make documentation available through the included ri command-line tool, install ruby-docs. You can then query the docs with: ri Array, ri Array.pop etc. (much like man-pages)

RubyGems

gem is a package manager 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.

Usage

To see what gems are installed:

$ gem list

To get information about a gem:

$ gem spec gem_name

By default, gem list and gem spec use the --local option, which forces gem to search only the local system. This can be overridden with the --remote flag. Thus, to search for the mysql gem:

$ gem list --remote mysql

To install a 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

To update all installed gems:

$ gem update

Running as normal user

When running gem as a normal user, gems are installed into ~/.gem instead of system-wide. This is considered the best way to manage gems on Arch. Unfortunately, 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 (compiled C code). This per-user behavior is enabled via /etc/gemrc and can be overridden by a ~/.gemrc file.

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

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.

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

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 managing gems with gem, you can use pacman, or some AUR helper. Ruby packages follow the naming convention ruby-[gemname]. This option provides the following advantages:

  • Gems are updated along with the rest of your system. As a result, you never need to run gem update: # pacman -Syu suffices.
  • Installed gems are available system-wide, instead of being available only to the user who installed them.

If a gem is not available in the repositories, you can use pacgemAUR to automatically create a package, which can then be installed by pacman.

See also