4
votes

I use rbenv but other team members use RVM.

When specifying the ruby 2.x.x version in the Gemfile I’ve been doing this:

ruby ENV['RBENV_VERSION'] || '2.2.4'

which grabs the current version I’m using from rbenv and uses it for the Gemfile. However, in production, it is not present and uses the specified version after ||.

I’ve been searching for a similar way to do this in RVM, the ultimate goal being to set up a Gemfile where all developers can use their local version of Ruby and a concrete version is specified for production.

This would allow developers to use rbenv or RVM for a project as well as not need to install new versions of Ruby every time they work on a project with a version they don’t have installed.

Are there any RVM users that can give me an equivalent to ENV['RBENV_VERSION'] for RVM? I’ve been searching a lot and haven’t found anything I like.

The best answer I can see is having RVM users specify the version via an environment variable name agreed upon by the team and used like "Specifying a Ruby version via the environment" as well and use that instead in the Gemfile.

1
Given that the specific ruby version 2.2.4 is only intended for production, and not development, why is this in your Gemfile in the first place? Why not put it somewhere more sensible, such as config/deploy.rb? - Tom Lord
I'm deploying to Heroku and as their docs indicate, I'm putting the ruby version in the Gemfile shown here. Even if I were to put the version in a different place, that doesn't answer my question. That would only move the problem to a different file :) - bideowego
Ahh ok, I understand your problem now. (BTW, my suggestion was answering the question -- the issue is still all about separating the development vs production config.) How about using github.com/bkeepers/dotenv to store the development ruby version? Would that work? - Tom Lord
Creating custom ENV variables is certainly an option. I was searching through the RVM docs to find something similar to the RBENV environment variabes. Thought I might find a more simple answer I was missing from RVM users. Ideally it should just be as simple as a single line in the Gemfile regardless of the version manager an dev on the team is using. - bideowego
But what if the developer is using chruby? Or just a plain old system ruby? The only catch-all would be to specify RUBY_VERSION in the Gemfile, but that's explicitly what the Heroku docs say not to do! So instead, you could do something like ruby (ENV['DEVELOPMENT_MODE'] ? RUBY_VERSION : '2.2.4') ... But now since we're setting an ENV variable flag, might as well just make it for the ruby version! ruby ENV['DEVELOPMENT_RUBY_VERSION'] || '2.2.4' ..... Unless I'm missing something here? - Tom Lord

1 Answers

1
votes

To get the current Ruby version from any RVM instance, you can query it using this method:

rvm list default string | sed s/ruby-//

You can also use the RVM prompt tools to query the current version

rvm-prompt i v | sed s/ruby-//

or

rvm-prompt i v p g | sed s/ruby-//

depending on how detailed you want to allow. Docs for the rvm-prompt command can be found in "rvm-prompt".

If the user chooses to leave RVM at its default version, you get that version reported, but if the Ruby version has been changed in RVM, you get the currently chosen version.

You can chain this in any way that ENV['RBENV_VERSION'] is used, such as:

`rvm list default string | sed s/ruby-//` || 2.2.4

This will choose the current RVM Ruby version or 2.2.4 if RVM doesn't show a version.

Note that RVM has to exist (or at least something called rvm has to be available as an executable). If not, an additional script wrapper may be needed to handle the situation in which it doesn't exist and degrade gracefully.

Personally, I love having it available everywhere, including production environments. It's the very first thing that I install on a new OS instance, even before the text editor, and the second thing that I do is install the appropriate Ruby version with RVM.