Ruby Version Management (rbenv, RVM)
Because they enable the installation and maintenance of numerous Ruby interpreters and their accompanying libraries a best practice Ruby version management libraries are essential tools for developers. These utilities, like rbenv and RVM, are particularly useful for moving between environments because most software projects are written against a known Ruby version.
Here is a description of rbenv and RVM (Ruby Version Manager) with code samples:
You can also read TK GUI in Ruby: Creating Your First Graphical User Interface
RVM (Ruby Version Manager)
Installing and managing several Ruby versions is made easy with RVM, a command-line utility. Because it allows for the management of several Ruby versions and the installation of additional Ruby packages, it is considered as the most widely used method for installing Ruby on Mac OS X.
RVM Key Commands and Installation
The install command can be used to install Ruby versions directly:
rvm install <version>installs a specific Ruby version (e.g.,rvm install 2.3.1).rvm listshows installed versions and which one is currently set for use.rvm use <version>allows you to change between installed Ruby versions (e.g.,rvm use 2.3.0).
Code Example: Listing Installed Versions with RVM
The status of installed Ruby interpreters can be seen using the rvm list command. The output frequently indicates the current and default version using symbols like *=:
puts <<~OUTPUT
user@dev:~ rvm list
rvm rubies
=* ruby-2.3.1 [ x86_64 ] # => - current
# =* - current && default
# * - default
OUTPUT
RVM Gemsets
Gemsets, which enable developers to construct separate collections of gems for various projects and help avoid dependency conflicts, are supported by RVM. To construct a gemset, a .rvmrc file must be generated.
Code Example: Creating a Gemset
By providing the new gemset name and the necessary Ruby version, the following program generates a .rvmrc file in the application’s root directory:
rvm --rvmrc --create ruby-2.2.2@myblog
You can see a list of available gemsets using the command rvm list gemsets.
You can also read Ruby GUI Toolkits: GTK, wxRuby And RubyCocoa Explained
rbenv
Another version management tool for installing and managing several Ruby versions is called rbenv. The ruby-build plugin is the most straightforward and suggested method for managing versions with rbenv.
rbenv Installation and Initialization
You usually clone the ruby-build plugin and the rbenv repository to your home directory in order to set up rbenv:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Making sure rbenv is initialized in your shell session after cloning requires adding the initialization code to configuration files such as.bash_profile or .zshrc.
Code Example: rbenv Shell Initialization
After determining whether rbenv is available, this code initializes it:
type rbenv > /dev/null
if [ "?" = "0" ]; then
eval "(rbenv init -)"
fi
On OS X, rbenv can also be installed using Homebrew: brew update followed by brew install rbenv.
You can also read Extending Ruby with C: A Complete Beginner’s Guide
rbenv Version Management Commands
You may control versions at the global and project levels withrbenv:
rbenv install --listlists the Ruby versions available for installation.rbenv install 2.2.0installs a chosen Ruby version.rbenv global 2.2.0marks a version as the global version used by the system by default.rbenv local 2.1.2specifies a local project version, which creates a.ruby-versionfile in the current directory with the specified version.rbenv global systemreverts the global version back to the system default (usually the one first in your path).
Code Example: Setting and Checking Global Version (rbenv)
rbenv global 2.2.0
rbenv global
2.2.0
Code Example: Setting Local Project Version (rbenv)
rbenv local 2.1.2
=> (Creates a .ruby-version file at the current directory with the specified version)
Uninstalling Ruby with rbenv
Two methods exist for uninstalling a Ruby version:
- Simply remove the directory from
~/.rbenv/versions:rm -rf ~/.rbenv/versions/2.1.0 - Use the
uninstallcommand, which achieves the same result:rbenv uninstall 2.1.0
You can also read Embedding Ruby: Running Ruby Code Inside C & C++ Programs
