chruby not changing to the proper version of ruby according to the value in .ruby-version on new terminal tab

2.5k Views Asked by At

I am using source /usr/local/opt/chruby/share/chruby/auto.sh to auto change ruby to the version in .ruby-version, but unfortunately this only works on cd.

When I open an new tab in my mac terminal, it opens to the same directory I was in in the previous tab, but auto change does not work. My solution to this is to cd .. and the cd back in, which triggers the change. This is tiresome, obviously.

I tried to manually call the function chruby_auto in the new tab, but this has no effect.

Can someone offer a solution of how to get my ruby version updated in this case? I would prefer an automatic solution, but will settle for a command to call.

2

There are 2 best solutions below

0
On BEST ANSWER

Actually nevermind, I traced this back to an issue in the order of these calls in my .bashrc

Incorrect:

source /usr/local/opt/chruby/share/chruby/chruby.sh
source /usr/local/opt/chruby/share/chruby/auto.sh
chruby ruby-2.3.0 

I turns out you must call the auto line AFTER the default line. Note that the default line must be after the chruby.sh line, as well.

Correct:

source /usr/local/opt/chruby/share/chruby/chruby.sh
chruby ruby-2.3.0 # default order important, if comes after auto.sh, will undo the auto change
source /usr/local/opt/chruby/share/chruby/auto.sh

So just leaving this answer here for posterity I guess

1
On

Unless I'm misunderstanding your issue, it sounds like you want a ~/.ruby-version file (different to the .ruby-version file in your project), so that you can determine what version of Ruby you want to use by default when opening a new terminal window (this value would then be overridden by a project .ruby-version file if it exists). For example, in my ~/.bashrc, I have:

if [[ -e /usr/local/share/chruby ]]; then
  # Load chruby
  source /usr/local/share/chruby/chruby.sh
  # Allow auto-switching of Ruby version when
  # directory has a .ruby-version file
  source /usr/local/share/chruby/auto.sh
fi

My ~/.ruby-version just contains 2.3.1, and there isn't a need to call chruby explicitly in ~/.bashrc. For more information, see the Default Ruby section of chruby's README file.