Difference between revisions of "Ruby"

From ArchWiki
Jump to: navigation, search
(Managing RubyGems using pacman: Rewrote section. Removed warning about using the "--no-user-install" option in PKGBUILDs. (see discussion page))
m (See also: fix link formatting)
 
(57 intermediate revisions by 20 users not shown)
Line 1: Line 1:
[[Category:Programming language]]
+
[[Category:Programming languages]]
 
[[ja:Ruby]]
 
[[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. 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 ===
+
For the latest version of Ruby, [[install]] the {{Pkg|ruby}} package. It includes [[#RubyGems|RubyGems]].
To install Ruby 2.0.0, install {{Pkg|ruby}}. Ruby 2.0 includes [[#RubyGems]].
+
  
=== Ruby 1.9 ===
+
=== Older versions ===
To install Ruby 1.9.3, install {{Pkg|ruby1.9}}. Ruby 1.9 includes RubyGems.
+
  
Pros:
+
* For Ruby 2.2, install the {{AUR|ruby2.2}} package.
* Vastly improved performance over 1.8
+
* For Ruby 2.1, install the {{Pkg|ruby2.1}}{{Broken package link|{{aur-mirror|ruby2.1}}}} package.
* New features for concurrency such as fibers.
+
* For Ruby 2.0, install the {{AUR|ruby2.0}} package.
* Various other language improvements, such as an improved CSV parser.
+
  
Cons:
+
=== Multiple versions ===
* 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.}}
+
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]], {{AUR|chruby}} or [[rbenv]].
  
=== Ruby 1.8 ===
+
=== Documentation ===
To install Ruby 1.8.7, install {{AUR|ruby1.8}} or {{AUR|ruby-1.8.7-svn}} from the [[AUR]]. Ruby 1.8 does not include RubyGems. Instead, it is available through the {{AUR|rubygems1.8}} package.
+
  
=== 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. 2.0.0-p0 and 1.9.3-p392), 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 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.
+
 
 +
RubyGems is a package manager for Ruby modules (called ''gems''), somewhat comparable to what [[pacman]] is to Arch Linux. It is included in the {{pkg|ruby}} package.
 +
 
 +
=== Setup ===
 +
 
 +
Before you use RubyGems, you should add {{ic|$(ruby -e "print Gem.user_dir")/bin}} to your {{ic|$PATH}}. You can do this by adding the following line to {{ic|~/.bashrc}}:
 +
PATH="$(ruby -e 'print Gem.user_dir')/bin:$PATH"
 +
 
 +
This is required for executable gems to work without typing out the full location, although libraries will work without having to modify your path.
 +
 
 +
If the method above does not work, you can try adding these lines at the end of your shell configuration file instead:
 +
 
 +
  #Setting the GEM_PATH and GEM_HOME variables may not be necessary, check 'gem env' output to verify whether both variables already exist
 +
  GEM_HOME=$(ls -t -U | ruby -e 'puts Gem.user_dir')
 +
  GEM_PATH=$GEM_HOME
 +
  export PATH=$PATH:$GEM_HOME/bin
  
 
=== Usage ===
 
=== Usage ===
 +
 
To see what gems are installed:
 
To see what gems are installed:
 
  $ gem list
 
  $ gem list
  
 
To get information about a gem:
 
To get information about a gem:
  $ gem spec <gem_name>
+
  $ 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:
 
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:
Line 46: Line 56:
  
 
The process can be sped up somewhat if you do not need local documentation:
 
The process can be sped up somewhat if you do not need local documentation:
  $ gem install mysql --no-rdoc --no-ri
+
  $ gem install mysql --no-document
 +
 
 +
{{Note|This can be made the default option by configuring the following {{ic|~/.gemrc}} file:
 +
{{hc|~/.gemrc|<nowiki>
 +
gem: --no-document
 +
</nowiki>}}
 +
}}
  
 
To update all installed gems:
 
To update all installed gems:
 
  $ gem update
 
  $ gem update
  
=== Running as normal user ===
+
=== Installing gems per-user or system-wide ===
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.
+
  
To use gems which install binaries, you need to add {{ic|~/.gem/ruby/2.0.0/bin}} to your {{ic|$PATH}}.
+
By default in Arch Linux, when running {{ic|gem}}, gems are installed per-user (into {{ic|~/.gem/ruby/}}), instead of system-wide (into {{ic|/usr/lib/ruby/gems/}}). This is considered the best way to manage gems on Arch, because otherwise they might interfere with gems installed by Pacman.
  
=== Running as root ===
+
Gems can be installed system wide by running the {{ic|gem}} command as root, appended with the {{ic|--no-user-install}} flag. This flag can be set as default by replacing {{ic|--user-install}} by {{ic|--no-user-install}} in {{ic|/etc/gemrc}} (system-wide) or {{ic|~/.gemrc}} (per-user, overrides system-wide).
When running as root, the gems will be installed into {{ic|/root/.gems}} and will '''not''' be installed to {{ic|/usr/lib/ruby/gems/}}.
+
  
{{Note|See bug #[https://bugs.archlinux.org/task/33327 33327] for more information.}}
+
[[#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.
+
  
 
=== Bundler ===
 
=== Bundler ===
[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.
+
 
 +
[http://bundler.io 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.
  
 
To install Bundler:
 
To install Bundler:
Line 70: Line 83:
  
 
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}}:
 
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=$(ruby -e 'print Gem.user_dir')
  
 
To start a new bundle:
 
To start a new bundle:
Line 91: Line 104:
  
 
# "This will automatically discover your Gemfile, and make all of the gems in
 
# "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
+
# your Gemfile available to Ruby." http://bundler.io/rationale.html
require 'rubygems'
+
 
require 'bundler/setup'
 
require 'bundler/setup'
  
Line 99: Line 111:
  
 
Finally, run your program:
 
Finally, run your program:
{{bc|bundle exec <main_executable_name.rb>}}
+
bundle exec ''main_executable_name.rb''
  
== Managing RubyGems using pacman ==
+
=== 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:
 
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.
+
* 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.
 
* 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.
+
If a gem is not available in AUR, you can use {{AUR|gem2arch}} or {{AUR|pacgem}} to automatically create a package, which can then be installed by pacman.
 +
 
 +
==== Quarry ====
 +
 
 +
Quarry is an opensource tool (GPL3 license) that allows to maintain [http://rubygems.org rubygems] binary repository for Arch Linux, as an easier alternative to building packages manually from the AUR. The source is hosted at [https://github.com/anatol/quarry github].
 +
 
 +
The repository is maintained by Arch developer anatolik at http://pkgbuild.com/~anatolik/quarry/, and is currently for the x86_64 architecture only. It contains many popular gems and new gems can be added upon request.
 +
 
 +
See [[Unofficial user repositories#quarry]] to enable it.
 +
 
 +
Then install required gem {{ic|# pacman -S ruby-$gemname}}.
 +
 
 +
If you have general questions - send it at the project announcement https://bbs.archlinux.org/viewtopic.php?id=182729<br>
 +
If you have bugreports or code improvements - file at github https://github.com/anatol/quarry
  
 
== 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
+
* Bundler - http://bundler.io/
* Bundler - http://github.com/carlhuda/bundler
+
* [[wikipedia:Why's_(poignant)_Guide_to_Ruby|why's (poignant) Guide to Ruby]]
 +
* [http://ruby.learncodethehardway.org/ Learn Ruby The Hard Way]

Latest revision as of 09:44, 3 November 2016

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

Installing Ruby

For the latest version of Ruby, install the ruby package. It includes RubyGems.

Older versions

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, chrubyAUR 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

RubyGems is a package manager for Ruby modules (called gems), somewhat comparable to what pacman is to Arch Linux. It is included in the ruby package.

Setup

Before you use RubyGems, you should add $(ruby -e "print Gem.user_dir")/bin to your $PATH. You can do this by adding the following line to ~/.bashrc:

PATH="$(ruby -e 'print Gem.user_dir')/bin:$PATH"

This is required for executable gems to work without typing out the full location, although libraries will work without having to modify your path.

If the method above does not work, you can try adding these lines at the end of your shell configuration file instead:

 #Setting the GEM_PATH and GEM_HOME variables may not be necessary, check 'gem env' output to verify whether both variables already exist 
 GEM_HOME=$(ls -t -U | ruby -e 'puts Gem.user_dir')
 GEM_PATH=$GEM_HOME
 export PATH=$PATH:$GEM_HOME/bin

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-document
Note: This can be made the default option by configuring the following ~/.gemrc file:
~/.gemrc
gem: --no-document

To update all installed gems:

$ gem update

Installing gems per-user or system-wide

By default in Arch Linux, when running gem, gems are installed per-user (into ~/.gem/ruby/), instead of system-wide (into /usr/lib/ruby/gems/). This is considered the best way to manage gems on Arch, because otherwise they might interfere with gems installed by Pacman.

Gems can be installed system wide by running the gem command as root, appended with the --no-user-install flag. This flag can be set as default by replacing --user-install by --no-user-install in /etc/gemrc (system-wide) or ~/.gemrc (per-user, overrides system-wide).

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=$(ruby -e 'print Gem.user_dir')

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://bundler.io/rationale.html
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 AUR, you can use gem2archAUR or pacgemAUR to automatically create a package, which can then be installed by pacman.

Quarry

Quarry is an opensource tool (GPL3 license) that allows to maintain rubygems binary repository for Arch Linux, as an easier alternative to building packages manually from the AUR. The source is hosted at github.

The repository is maintained by Arch developer anatolik at http://pkgbuild.com/~anatolik/quarry/, and is currently for the x86_64 architecture only. It contains many popular gems and new gems can be added upon request.

See Unofficial user repositories#quarry to enable it.

Then install required gem # pacman -S ruby-$gemname.

If you have general questions - send it at the project announcement https://bbs.archlinux.org/viewtopic.php?id=182729
If you have bugreports or code improvements - file at github https://github.com/anatol/quarry

See also