Set Up Your Ruby Development Environment

An appropriate IDE is the base of comfortably writing Ruby scripts. Writing a good Ruby script also needs some Ruby tools, Ruby version manager, package manger and dependency manger, etc.
I will introduce how to create our own Ruby development environment.First we should know is the Ruby tools.

Ruby tools

rbenv, manages Ruby versions

In Mac OS, the ruby is a built-in tool. Some system scripts rely on the system ruby version. The system ruby version normally is old and we don’t have the permission to install gems into the system directory. So we cannot correctly execute the gem install xx command unless using the sudo. But running sudo command might break our system security.
If we want a new version Ruby or installing other gems, we need a Ruby version manager.
The rbenv can help us do this.

  1. Install rbenv.
1
brew install rbenv
  1. Set up rbenv in your shell.
1
rbenv init
  1. Close your Terminal window and open a new one so your changes take effect.
  2. List latest stable versions.
1
rbenv install -l
  1. Install a Ruby version.
1
rbenv install 2.6.8
  1. Specify global Ruby version
1
rbenv global 2.6.8
  1. Check ruby version
1
2
which ruby
# /Users/xxxxx/.rbenv/shims/ruby

gem, Ruby packages

The ruby package repository. You can find nearly any tools you want.
Popular gems:

bundler, Ruby project dependency manager

The bundler is a gem that you can execute gem install bundler to install it.
It’s a dependency manager. When writing a Ruby script, we might need to import some gems into our scripts. Our scripts rely on the special Ruby version and gems version. These gems also have their own dependencies and we cannot manage them manually.
The bundler can generate a Gemfile to specify our script’s dependencies.

  1. Using the command bundle init to initialize a Gemfile at current directory.Then you can edit the Gemfile to set up the gem dependencies.
  2. Next, execute bundle install to instal all dependencies.

VSCode configuration

The basic VSCode Ruby extensions

Ruby

This extension provides enhanced Ruby language and debugging support for Visual Studio Code.

Ruby Solargraph

Solargraph is a language server that provides intellisense, code completion, and inline documentation for Ruby.

Before install Ruby Solargraph, we need to install two gems, solargraph and rubocop.

The Ruby Solargraph relies on yard documentation. Some gems include it, some do not. Fortunately, we can generate the missing files by running yard gems. Restart or reload you VSCode and then you would find all code completion suggestions are came back.

Endwise

This is an extension that wisely adds the “end” keyword to code structures in languages like Ruby or Crystal while keeping the correct indentation levels.

Error Lens

ErrorLens turbo-charges language diagnostic features by making diagnostics stand out more prominently, highlighting the entire line wherever a diagnostic is generated by the language and also prints the message inline.

VSCode settings

1
2
3
4
5
6
7
8
9
10
{
"ruby.lint": {
"rubocop": true
},
"ruby.useLanguageServer": true,
"solargraph.formatting": true,
"[ruby]": {
"editor.defaultFormatter": "castwide.solargraph"
}
}