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.
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.
rbenv can help us do this.
- Install rbenv.
brew install rbenv
- Set up rbenv in your shell.
- Close your Terminal window and open a new one so your changes take effect.
- List latest stable versions.
rbenv install -l
- Install a Ruby version.
rbenv install 2.6.8
- Specify global Ruby version
rbenv global 2.6.8
- Check ruby version
The ruby package repository. You can find nearly any tools you want.
- HTTP request: faraday
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.
- Using the command
bundle initto initialize a
Gemfileat current directory.Then you can edit the Gemfile to set up the gem dependencies.
- Next, execute
bundle installto instal all dependencies.
The basic VSCode Ruby extensions
This extension provides enhanced Ruby language and debugging support for Visual Studio Code.
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,
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.
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.
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.