Working with Ruby

As a web developer, in some of my projects I must work with Ruby. This is a little bit weird for me (I usually work with PHP), and after some headaches, here you have a simple dummy guide to work with Ruby.

Installing multiple versions

Some projects will need version ‘A’; some others version ‘B’. This is more usual than you are thinking. The solution is very easy: install Ruby Version Manager. This can be done in two different ways.

The next command should be always executed:

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

Then, if AWS is working (it’s currently down as I’m writing this story), type the following command:

\curl -sSL https://get.rvm.io | bash -s stable

If this fails (because of AWS of whatever reason), you can try to do it in an “offline mode”:

curl -sSL https://github.com/rvm/rvm/tarball/stable -o rvm-stable.tar.gz
mkdir rvm && cd rvm
tar --strip-components=1 -xzf ../rvm-stable.tar.gz
./install --auto-dotfiles
source ~/.rvm/scripts/rvm

That’s all. The next step is installing the version you need. For example, Ruby 2.2:

rvm install ruby-2.2

You can list all your available versions with the following command:

rvm list

And, in order to select a default version:

rvm --default use 2.2.6

Working with Ruby dependencies

If you come from the JavaScript world, you should know about npm + Yarn. Also, if you come from PHP, you should know about Composer. Ruby does have it’s own package (gem) manager called RubyGems. With the following command you can install gems in your computer:

gem install name-of-gem

This is not how you should work, installing manually gems one by one. Instead of that, somebody created a gem called bundler which works like Yarn. First of all, install the bundler gem (only once):

gem install bundler

After this, you must declare your dependencies in a file called Gemfile. This is an example to use Capistrano with a Symfony project:

# Pull gems from RubyGems
source 'https://rubygems.org'

gem 'capistrano', '~> 3.1'
gem 'capistrano-symfony', '~> 1.0.0.rc1'

Now, you must run bundle install to install the needed gems and this will generate a file called Gemfile.lock. This file will tell bundler to install the exact versions named in that file, so everyone in your team (or just you, changing your computer) use the same (tested) gem versions.

With the Gemfile.lock file created, you can use the bundle install command to read that file and install the exact versions, or you can also run bundle update to ignore that file and update it with the latest versions, always following what the Gemfile is saying, indeed!

Read about how to deploy a Symfony project with Capistrano in my older story: https://medium.com/@jontorrado/deploying-a-symfony-application-with-capistrano-a954a1a03819

Show your support

Clapping shows how much you appreciated Jon Torrado’s story.