使用 Gemfile 进入文件夹时如何调用 RVM?

Jik*_*ose 5 ruby shell command-line rvm

我很惊讶 RVM 如何通过命令行导航到目录来切换尊重 Gemfile 的 Ruby 版本?RVM 是否通过 shell 收到回调?任何人都可以提供有关此的指示吗?

例如这样的消息:

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 /directory/path/to/Gemfile'.
To ignore the warning for all files run 'rvm rvmrc warning ignore allGemfiles'.
Run Code Online (Sandbox Code Playgroud)

7st*_*tud 2

根据这篇文章:

https://unix.stackexchange.com/questions/21363/execute-bash-scripts-on-entering-a-directory

rvm 重新定义了 cd 命令。如果我按照阿马丹的建议去做,我会得到:

~$ type cd
cd is a function
cd () 
{ 
    __zsh_like_cd cd "$@"
}
Run Code Online (Sandbox Code Playgroud)

这看起来像是某种别名,所以让我们尝试一下:

~$ type __zsh_like_cd
__zsh_like_cd is a function
__zsh_like_cd () 
{ 
    typeset __zsh_like_cd_hook;
    if builtin "$@"; then
        shift || true;
        for __zsh_like_cd_hook in chpwd "${chpwd_functions[@]}";
        do
            if typeset -f "$__zsh_like_cd_hook" > /dev/null 2>&1; then
                "$__zsh_like_cd_hook" "$@" || break;
            fi;
        done;
        true;
    else
        return $?;
    fi
}
Run Code Online (Sandbox Code Playgroud)

在 zsh shell 中,chpwd 是一个钩子函数,当当前目录更改时调用。但是,我不确定为什么它可以在 bash shell 中工作,因为它不提供 chpwd 挂钩函数。阿马丹?