Should RVM automatically change gemsets based on .ruby-gemset? - ruby

I updated RVM and started switching from .rvmrc to .ruby-gemset and, where necessary, .ruby-version.
However, so far, when I change into a directory with .ruby-gemset, it doesn't automatically start using the correct one as it would with .rvmrc.
Is it supposed to work the same way?
I'm using RVM 1.20.5.

RVM detects .ruby-version and only if it is present it will read .ruby-gemset.
RVM does not reads .ruby-gemset when .ruby-version is not available.
You could try .ruby-version (editted from current):
default

Related

Ruby Version Different Depending on Terminal Tab

This question seems to happen a lot (here, here, and here), but never with a successful answer.
I have ruby 2.5.0 installed correctly and have verified it with ruby -v. However, when I open a new Terminal tab, it defaults me back to ruby 2.3.3p222 for some reason. I have ruby '~> 2.5.0' in my gemfile, if it helps.
Does anyone know how this is happening?
RVM can read the .ruby-version file on the root of your project folder.
echo 2.5.0 > .ruby-version
Open a new terminal and check the Ruby version.
A new tab opens a new shell, which re-reads your shell's initialization files. It doesn't start with the environment variables which happen to be set in the currently-active tab.
If you are using rbenv, you can set your global ruby by:
rbenv global 2.5.0
Pretty much the same thing for rvm:
rvm --default use 2.5.0

warning from ruby version file

I cloned a project and it had a .ruby-version file to specify ruby version of the project. content:
2.0.0
when I switched into the project dir for the first time it gave me warning:
ruby-2.0.0-p645 is not installed. To install do: 'rvm install
ruby-2.0.0-p645'
However it's true that my system didn't have Ruby 2.0 but Ruby 2.2 instead. Does that warning makes sense? Is there a way I use duplicate functionality of (~>) gem dependency in ruby-version file?
Based on the warning, you are using rvm. As another answer says, rbenv can also use a .ruby-version file, but switching to rbenv isn't neccesary, and won't really change anything.
I don't think rvm's .ruby-version file supports a range of versions. It needs to specify a version name that could be passed to rvm use, and I don't think there's any way to say a range of versions there.
You can:
Install ruby 2.0.0 with rvm: rvm install 2.0.0. When you switch into the project directory, rvm will automatically switch you to using ruby 2.0.0 like the .ruby-version file directs.
Simply change your .ruby-version file to specify the version of ruby you want to use instead. Just edit it in a text editor, or run rvm --ruby-version use 2.2.3 to have it write the .ruby-version file for you. Now when you switch into the project directory, rvm will automatically switch you to 2.2.3.
Delete the .ruby-version file entirely. You aren't required to use it. Personally, I don't use them and don't find them helpful. The .ruby-version file is meant to force a particular version of ruby to be used in that project -- but most projects work with multiple versions of ruby, and this isn't really neccesary, and as you've seen can be a headache when it's out of date and is trying to force you to use an older version of ruby, when a newer one would probably work fine.
.ruby_version is used for rbenv
You should install rbenv
or create .rvmrc for rvm

Set a default Ruby version for a specific directory (RVM)

How can I use RVM to set a default ruby version for a certain directory? So that every time I cd into that directory, it switches to my preferred version of Ruby.
Directly from the RVM documentation:
RVM supports multiple files allowing to configure a project for automated ruby switching. In any case make sure to add those files to your version control systems as it is part of the project configuration.
Listed in order of precedence:
.rvmrc - shell script allowing full customization of the environment,
.versions.conf - key=value configuration file
.ruby-version - single line ruby-version only
Gemfile" - comment: #ruby=1.9.3 and directive: ruby "1.9.3"
One way is to use a Gemfile and set the ruby version in it. like so:
ruby '2.2.0'
then when you enter the directory you will see the following message from rvm
RVM used your Gemfile for selecting Ruby, it is all fine - Heroku does that too,
you can ignore these warnings with 'rvm rvmrc warning ignore /Users/danmanstx/rails_projects/app/Gemfile'.
To ignore the warning for all files run 'rvm rvmrc warning ignore allGemfiles'.
Create a .ruby-version file in that directory with your version information. To set version as 2.1.2 for a directory, create the file with only "2.1.2" as the content.
$ cat .ruby-version
2.1.2
in rails 6 you only have to change what's inside of .ruby-version in your app folder
Or simply use rvm --ruby-version ruby_version#gemset --create , --create here will create the gemset if it is not present already. If you don't need to specify a gemset, but instead use the default gemset. leave out #gemset --create. ruby_version for example 2.0.0.

Understanding and using project specific .rvmrc rvm config files

Just getting started with Ruby.
I am trying to use rvm. Now, for Project A I am trying to specify a specify Ruby version and a gemset.
$ cat projecta/.rvmrc
rvm 1.8.7#projecta
My understanding is that the part before # specifies the Ruby version and the the part after # specifies the gemset name. A gemset IMU is to provide a project specific isolated location where you can install gems.
After I check-in this project, what can I do to automate the process of creating the gemset and installing the correct Ruby version for someone else checking the project out at a later date?
Please suggest appropriate alternatives, since I am just getting started with Ruby today.
The Old Way
to make sure gemset / ruby is available use this .rvmrc:
rvm use 1.8.7#projecta --install --create
It will install ruby if missing and create gemset if missing.
And a special note, please do not use 1.8.7, it's deprecated ruby, with almost no support (security patches till half of 2013), you should stick with latest available ruby:
rvm use ruby
which at this time is: 1.9.3-p194
Add On-Demand Syntax
Your syntax won't work as written. If you want to force people to compile rubies and create gemsets on demand, rather than being warned when things don't exist, you want a project .rvmrc file like this:
# Compile rubies on demand.
rvm_install_on_use_flag=1
# Create gemsets on demand.
rvm_gemset_create_on_use_flag=1
# Use ruby-1.8.7 while in project tree.
rvm use 1.8.7
# Use gemset "projecta" while in project tree.
rvm gemset use projecta
There are certainly other ways to do it, but this way makes everything explicit, and you can comment out individual lines if you need to do so.
See Also
https://rvm.io/workflow/rvmrc/

How to automatically start Ruby Version Manager

Is there a way to automatically load rvm on start up?
Every time I open a new terminal window I need to type rvm 1.9.2 to be able to use the gem set. Can I add 1.9.2 as a default?
There are several ways
The normal one is
rvm use 1.9.2 --default
You could also create a file .rvmrc which can also load a specific gemset per folder. For example if you have an application that uses the gemset 1.9.2#myapp, your .rvmrc in myapp could be:
# myapp/.rvmrc
rvm use 1.9.2#myapp --create
you should be able to simply do this;
rvm --default use 1.9.2

Resources