当您更改目录时,RVM 如何检测? [英] How does RVM detect when you've changed directories?

查看:43
本文介绍了当您更改目录时,RVM 如何检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RVM 可以做的一件事是在您更改目录时注意,查找 .rvmrc,并根据该文件的内容更改您当前的 Ruby 版本和 gemset.一旦您切换到该目录,它就会进行此更改并相应地输出一条消息.

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.

另一种方法可能是为 cd 设置别名以更改目录,但也检查 .rvmrc 并根据指示更改 gemset.

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

是否有用于更改目录的 Unix 系统挂钩,或者 RVM 是否使用了其他一些技巧?

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

推荐答案

[RVM] 如何检测当前工作目录的变化?

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

这完全取决于外壳甚至外壳版本.实现这一点的最简单方法是,正如所建议的那样,通过定义一个同名的函数来覆盖 cd.一定要调用内置的 cd 函数,否则你会导致 非常 讨厌或 非常 有趣的副作用(取决于视角).>

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}'."
}

通过将其放在文本源文件中并获取文件,您可以将此自定义 cd 功能加载到获取文件的会话中.

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'.

如果您希望这对您的用户帐户中的交互式 shell 始终有效,请将函数定义放入 ~/.bash_profile.或者,您可以从 ~/.bash_profile 获取存储函数的文本文件.您可以在 Bash 手册中阅读有关 Bash 启动文件的更多信息.

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.

Ruby 环境管理器 (RVM) 主要针对 Bash shell.RVM 将在 ZSH shell 中工作,它允许您在 cd 操作之后添加函数调用.这是迄今为止解决此任务的最佳方法.因此,如果您的 shell 允许,请务必使用此方法.RVM 确实如此.

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.

有关广泛的示例,您可以在 GitHub 上查看 RVM 的 cd 功能.

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

RVM 利用此功能加载每个项目(实际上是目录).rvmrc 文件 以及为用户提供配置各种钩子的能力.您可以在文档网站上阅读有关 RVM 挂钩的更多信息.

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.

这篇关于当您更改目录时,RVM 如何检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆