Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

internals - How does RVM detect when you've changed directories?

One thing that RVM can do is notice when you've changed directories, look for an .rvmrc, and change your current Ruby version and gemset according to that file's contents. It makes this change as soon as you change into that directory and outputs a message accordingly.

How does it detect a change in the current working directory? I can imagine an extremely crude way of doing this: run a daemon that runs as the current user and keeps checking the results of pwd. But I'm sure that's not what it does.

Another approach might be to alias cd to change directories but also check for an .rvmrc and change gemsets if so instructed.

Is there a Unix system hook for changing directories, or is there some other trick that RVM is using?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...