Getting Started with Ruby for JavaScript and Node Developers

Recently I have been brushing on some of the languages that I used to work with. After working with JavaScript for a while, I thought it might be interesting to make a guide for other JavaScript developers out there who may want to play with a different language like Ruby. In this guide we will go through setting up a dev environment for working with Ruby using rbenv and bundler.

TL;DR

  • rbenv: similar to nvm for managing different Ruby versions
  • rbenv install <Ruby version>: similar to nvm install <node version>
  • rbenv global <Ruby Version>: similar to nvm alias default <node version>
  • rbenv local <Ruby Version>: similar to nvm use <node version
  • rbenv versions: similar to nvm ls to see the list of installed Ruby versions

  • bundle init: similar to npm init -y to create a Gemfile

  • Gemfile: similar to package.json file to specify dependencies
  • bundle add <package>: similar to npm install <package> --save

  • gem install <package>: similar to npm install -g <package>

  • bundle install: similar to npm install
  • bundle install --path vendor --binstubs bin: to set up a local folder for packages/gems. This is done automatically by default in Node when you do npm install
  • bundle exec ruby main.rb: execute command in the context of the current bundle. This is done automatically by default in Node by the way modules are resolved.

Overview

  • Install rbenv and latest version of Ruby with rbenv
  • Install bundler for package management
  • Install packages locally with pre defined local Ruby version
  • Execute a simple script in the context of a project bundle

Installing rbenv and Ruby

You can install rbenv very easily with brew install rbenv. Afterwards, we need to install a version of Ruby. As of the time of writing this article, Ruby 2.5.0 is the latest version. You can run rbenv install 2.5.0 to build and install the given version of Ruby. After it's installed you can set it to be the default global version by rbenv global 2.5.0. Now if you open a new shell and do ruby -v you should get the right version in the output. If you see the version in the output it means you are good to go to move on to the next section.

Install bundler

After setting up rbenv and a global Ruby version, go ahead and run gem install bundler. It will install the latest version of bundler in the respective global version of Ruby and the global bundler binary will become availbale. Run bundle -v to verify that the bundler is being installed successfully.

Install Packages Locally

After installing bundler, let's set up a project folder. Run the following one after the other to set up your project:

mkdir -p ~/Desktop/ruby-project && cd $_  # <-- create project folder and go to the folder  
rbenv local 2.5.0 # <-- Specify that this project uses Ruby 2.5.0  
bundle init # <-- Create a Gemfile to specify packages  
bundle install --path vendor --binstubs bin # <-- set up .bundle config to install packages to the local folder  
touch .gitignore main.rb # <-- create a gitignore and the main file  

Open the .gitignore file and make sure to add the vendor and .bundle folders to the list of ignored folders:

.gitignore

vendor  
.bundle

Now we are ready to install our first package/gem. When you install a new Gem documentation files will be added automatically. If you want to skip that step system-wide you can add the following to your ~/.gemrc file:

gem: --no-document  

Next, run bundle add countries to install a Gem to help us with different country names and information. After running that you should be able to see the vendor folder being populated with new content. Also the Gemfile will include an entry like gem "countries", "~> 2.1" to specify our new dependency.

Now if you want to remove a package, simply remove the entry from the Gemfile and run bundle clean to remove unused packages. Also note that if you are given a Gemfile you can simply run bundle install or just bundle to install all the dependencies for the project.

Execute a Simple Script

After installing the Countries Gem, open the main.rb file and add the following:

require "countries"  
c = ISO3166::Country.new('US')  
puts c  

Then run the script with bundle exect ruby main.rb and you should see the following output:

United States of America  

If you see the input it means your Ruby environment has been set up properly and you are ready to roll!

Notes

When you specify bundle install --path vendor you tell Bundler to install all the dependencies to the local folder. If you don't specify the path option, Bundler is going to install your packages in the Ruby folder specified by your local Ruby version. The following is a path example to a package that have been installed to the rbenv folder:

~/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/test-unit-3.2.7

Conclusion

Ruby is a great scripting server-side language that has a variety of applications. I hope that this article can save you a little bit of headache when you want to set up a dev environment that you are used to when working with Node and npm.