Ruby: Difference between revisions
(→Setup: rw) |
(→Configuration: more elegant way to retrieve USER INSTALLATION DIRECTORY) |
||
(50 intermediate revisions by 30 users not shown) | |||
Line 4: | Line 4: | ||
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. | ||
== | == Installation == | ||
For the latest version of Ruby, [[install]] the {{Pkg|ruby}} package. | For the latest version of Ruby, [[install]] the {{Pkg|ruby}} package. | ||
To install IRB, [[install]] the {{Pkg|ruby-irb}} package. | |||
=== Multiple 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]], {{AUR|chruby}} | 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 one of [[RVM]], {{AUR|chruby}}, [[rbenv]], {{AUR|asdf-vm}}. | |||
=== Documentation === | === Documentation === | ||
To make documentation available through the | To make documentation available through the {{ic|ri}} command-line tool, | ||
install {{Pkg|ruby-rdoc}} and {{Pkg|ruby-docs}} for the documentation itself. | |||
You can then query the docs with: {{ic|ri Array}}, {{ic|ri Array.pop}} etc. (much like man-pages) | You can then query the docs with: {{ic|ri Array}}, {{ic|ri Array.pop}} etc. (much like man-pages) | ||
=== JRuby === | |||
The [[Java]] implementation of Ruby, [[Wikipedia:JRuby|JRuby]] can be installed with the {{Pkg|jruby}} package. | |||
== RubyGems == | == RubyGems == | ||
RubyGems is a package manager for Ruby modules (called ''gems''), somewhat comparable to what [[pacman]] is to Arch Linux. It | RubyGems is a package manager for Ruby modules (called ''gems''), | ||
somewhat comparable to what [[pacman]] is to Arch Linux. | |||
It can be installed with the {{pkg|rubygems}} package, which is a dependency of {{Pkg|ruby}}. | |||
=== | === Configuration === | ||
By default in Arch Linux, when running {{ic|gem}}, gems are installed per-user (into {{ic|~/.local/share/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. | |||
The recommended way to setup that is by manually specifying your {{ic|$GEM_HOME}}, which then can be [[append|appended]] to your {{ic|$PATH}} [[environment variable]] in order to allow RubyGems binaries to be executed: | |||
{{hc|1=~/.profile|2= | {{hc|1=~/.profile|2= | ||
export GEM_HOME="$(gem env user_gemhome)" | |||
export PATH="$PATH:$GEM_HOME/bin" | |||
}} | }} | ||
This is required for executable gems to work without typing out the full location, although libraries will work without having to modify your 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. | ||
{{Note|After saving the changes, restart the terminal for changes to apply.}} | |||
}} | |||
Use {{ic|gem env}} to view the current RubyGems environment: | Use {{ic|gem env}} to view the current RubyGems environment: | ||
Line 53: | Line 58: | ||
$ 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 | 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 mysql2 gem: | ||
$ gem list --remote | $ gem list --remote mysql2 | ||
To install a gem: | To install a gem: | ||
$ gem install | $ gem install mysql2 | ||
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 | $ gem install mysql2 --no-document | ||
{{Note|This can be made the default option by configuring the following {{ic|~/.gemrc}} file: | {{Note|This can be made the default option by configuring the following {{ic|~/.gemrc}} file: | ||
Line 71: | Line 76: | ||
$ gem update | $ gem update | ||
=== Installing gems | === Installing gems system-wide === | ||
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). | 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). | ||
Line 81: | Line 84: | ||
=== Bundler === | === Bundler === | ||
[ | [https://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: | ||
$ gem install bundler | $ gem install bundler | ||
To start a new bundle: | To start a new bundle: | ||
Line 95: | Line 95: | ||
{{hc|Gemfile| | {{hc|Gemfile| | ||
gem "rails", "3.2.9" | gem "rails", "3.2.9" | ||
gem " | gem "mysql2" | ||
}} | }} | ||
Line 102: | Line 102: | ||
Alternatively, run the following to install gems to {{ic|.bundle}} in the working directory: | Alternatively, run the following to install gems to {{ic|.bundle}} in the working directory: | ||
$ bundle | $ bundle config set --local path '.bundle' | ||
{{Note|command `bundle install --path .bundle` is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. But if above command is not valid, maybe your ruby version is old so you should use this command.}} | |||
Do not forget to edit your main executable: | |||
{{bc|#!/usr/bin/env ruby | {{bc|#!/usr/bin/env ruby | ||
# "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." | # your Gemfile available to Ruby." https://bundler.io/rationale.html | ||
require 'bundler/setup' | require 'bundler/setup' | ||
Line 115: | Line 117: | ||
Finally, run your program: | Finally, run your program: | ||
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 [[pacman]], or an [[AUR]] | Instead of managing gems with {{ic|gem}}, you can use [[pacman]], or an [[AUR helper]]. Ruby packages follow the naming convention ruby-''gemname''. | ||
This option provides the following advantages: | This option provides the following advantages: | ||
Line 129: | Line 132: | ||
==== Quarry ==== | ==== Quarry ==== | ||
Quarry is | Quarry is a tool that allows to maintain a [https://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 | The repository is maintained by the Arch developer anatolik at https://pkgbuild.com/~anatolik/quarry/. It contains many popular gems and new gems can be added upon request. | ||
See [[Unofficial user repositories#quarry]] to enable it. | See [[Unofficial user repositories#quarry]] to enable it. | ||
Then install required gem {{ic| | Then [[install]] the required gem. The name of the package is {{ic|ruby-''gem name''}}. | ||
General questions can be asked at https://bbs.archlinux.org/viewtopic.php?id=182729. | |||
== Interactive Shell == | |||
=== Pry === | |||
Pry is a powerful alternative to the standard IRB shell for Ruby. It features syntax highlighting, a flexible plugin architecture, runtime invocation and source and documentation browsing. | |||
$ gem install pry | |||
$ pry | |||
== See also == | == See also == | ||
* [[Ruby on Rails]] | * [[Ruby on Rails]] | ||
* Ruby - | * Ruby - https://www.ruby-lang.org/ | ||
* Bundler - | * Bundler - https://bundler.io/ | ||
* [[wikipedia:Why's_(poignant)_Guide_to_Ruby|why's (poignant) Guide to Ruby]] | * [[wikipedia:Why's_(poignant)_Guide_to_Ruby|why's (poignant) Guide to Ruby]] | ||
* [ | * [https://learnrubythehardway.org/book/ Learn Ruby The Hard Way] | ||
* [ | * [https://blog.hyfather.com/blog/2011/10/18/bundler/ Comparison of Bundler and RVM workflows] |
Latest revision as of 13:16, 14 July 2023
Ruby is a dynamic, interpreted, open source programming language with a focus on simplicity and productivity.
Installation
For the latest version of Ruby, install the ruby package.
To install IRB, install the ruby-irb 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 one of RVM, chrubyAUR, rbenv, asdf-vmAUR.
Documentation
To make documentation available through the ri
command-line tool,
install ruby-rdoc and ruby-docs for the documentation itself.
You can then query the docs with: ri Array
, ri Array.pop
etc. (much like man-pages)
JRuby
The Java implementation of Ruby, JRuby can be installed with the jruby package.
RubyGems
RubyGems is a package manager for Ruby modules (called gems), somewhat comparable to what pacman is to Arch Linux. It can be installed with the rubygems package, which is a dependency of ruby.
Configuration
By default in Arch Linux, when running gem
, gems are installed per-user (into ~/.local/share/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.
The recommended way to setup that is by manually specifying your $GEM_HOME
, which then can be appended to your $PATH
environment variable in order to allow RubyGems binaries to be executed:
~/.profile
export GEM_HOME="$(gem env user_gemhome)" export PATH="$PATH:$GEM_HOME/bin"
This is required for executable gems to work without typing out the full location, although libraries will work without having to modify your path.
Use gem env
to view the current RubyGems environment:
$ gem env
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 mysql2 gem:
$ gem list --remote mysql2
To install a gem:
$ gem install mysql2
The process can be sped up somewhat if you do not need local documentation:
$ gem install mysql2 --no-document
~/.gemrc
file:
~/.gemrc
gem: --no-document
To update all installed gems:
$ gem update
Installing gems system-wide
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
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 "mysql2"
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 config set --local path '.bundle'
Do not 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." https://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 an 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.
- Installed gems are available system-wide, instead of being available only to the user who installed them.
Quarry
Quarry is a tool that allows to maintain a 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 the Arch developer anatolik at https://pkgbuild.com/~anatolik/quarry/. It contains many popular gems and new gems can be added upon request.
See Unofficial user repositories#quarry to enable it.
Then install the required gem. The name of the package is ruby-gem name
.
General questions can be asked at https://bbs.archlinux.org/viewtopic.php?id=182729.
Interactive Shell
Pry
Pry is a powerful alternative to the standard IRB shell for Ruby. It features syntax highlighting, a flexible plugin architecture, runtime invocation and source and documentation browsing.
$ gem install pry $ pry