Managing Ruby Gems
Ruby gems are packages that add functionality to the core Ruby libraries. Each gem normally provides functionality in a specific area and can depend on other gems. Rails is a Ruby gem. Presumably, in the past, gem versioning caused problems for Ruby devs which has led to the development of a couple of tools:
RVM
RVM stands for Ruby Version Manager and allows a dev to switch betwen versions of Ruby. More importantly it can also sets of gems.
- Gemsets are associated with a particular version of Ruby.
- There is gemset named global (per Ruby version) and gems in this set are available to all other gemsets for that Ruby version.
- To associate a project with a particular gemset, include a .rvmrc file in the project root containing
rvm --create use version@project_name > /dev/null
This will create the gemset if it does not exist and switch to it automatically. - Useful commands to work with gemsets: rvm gemset [list|name|create|use|delete|empty|rename|copy]
Bundler
While RVM is used to manage and switch between installed gems, Bundler is a tool to install gems and associate them with a project.
- Bundler is itself a gem and is installed with Rails 3.
- Project dependencies are listed in a gemfile file in the project root.
- Gems versions can be specified in the gemfile but that is not really necessary with the gemfile.lock file which stores version numbers.
- bundle install ensures all gems listed in the gemfile are installed to the current gemset.
- Gems can be grouped by environment (development | test | production).
- Bundler ensures that the correct gems are ‘required’ by Ruby for the environment it is running under.
- The environment groups allow bundle install to be selective on what to install. e.g. for a production server
bundle install –without development test
RubyMine Gem Management
It looks as though RubyMine will be my environment of choice and it includes it’s own sdk (Ruby version) and gem management system that should integrate well with RVM’s gemsets and Bundler’s gemfile.
- A project can be associated with a particular version of Ruby and a gemset in RM’s settings pane.
- Once a gemset is selected, any gems installed by RM will be installed to this gemset.
- RM should also be able to read a .rvmrc file in the project root and switch automatically to the specified gemset. This currently (RM 3.0.1) seems not to work with newly created gemsets that have not yet had gems installed to them so best to check in the project settings to be sure.
- RM scans the gemfile, offering to install and attach and missing gems.
- Gems that are attached get scanned by RM to enable features such as autocomplete within the IDE.