How does RVM detect when you've changed directories?

假如想象 提交于 2019-11-30 02:03:19
Wayne E. Seguin

How does [RVM] detect a change in the current working directory?

This entirely depends on the shell and even shell version. The simplest method for accomplishing this is, as was suggested, overriding cd by defining a function of the same name. Be sure to call the built in cd function or you will cause either very nasty or very amusing side effects (depending on perspective).

cd() {
  builtin cd $*
  echo "Changed directories from '${OLDPWD}' to '${PWD}'."
}

By placing this in a text source file and sourcing the file you can load this custom cd functionality into the session sourcing the file.

wayneeseguin$ cd /tmp
Changed directories from '/home/wayneeseguin' to '/tmp'.

If you would like this to always be in effect for interactive shells in your user account either place the function definition into ~/.bash_profile. Alternatively you can source the text file in which you stored the function in from ~/.bash_profile. You can read more about Bash startup files in the Bash manual.

The Ruby enVironment Manager (RVM) is written in and targeted mainly for the Bash shell. RVM will work in the ZSH shell which allows you to add function calls to after a cd action. This is by far the best way to approach solving this task. So, if your shell allows for it, by all means use this method. RVM does.

For an extensive example you can look at RVM's cd functionality on GitHub.

RVM utilizes this functionality for loading per-project (directory, really) .rvmrc files as well as providing users with the ability to configure various hooks. You can read more about RVM's hooks on the documentation website.

When you install rvm you have to add:

[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm

to your shell config (usually .bashrc or .zshrc).

This loads RVM when you start a shell which then overrides your cd command and checks for .rvmrcs as you move around. See the contents of $HOME/.rvm/scripts/cd for more details.

Does it just look at the current directory whenever it needs to resolve the contents of the gemset? Is there something that makes you think it's doing this in parallel with changing directories, rather than on demand?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!