ruby-on-rails 打开终端时的自适应Ruby版本

aamkag61  于 2023-10-21  发布在  Ruby
关注(0)|答案(2)|浏览(143)

这个问题与this one相反(也问hereherehere
我安装了两个版本的Ruby

ubuntu:~/environment $ rvm list

       ruby-2.6.6 [ x86_64 ]
    =* ruby-3.0.2 [ x86_64 ]

每次我打开一个终端窗口,ruby-3.0.2(默认值)都被设置。问题是,对于我的一些老项目,我必须使用ruby-2.6.6,所以每次我都必须切换到

rvm use 2.6.6

当我打开特定项目的终端窗口时,有没有办法自动选择ruby 2.6.6?我曾尝试用.ruby-version文件覆盖默认的rvm版本(如建议的here),但它没有做到这一点。

  • 编辑 * 文件/home/ubuntu/.rvm/scripts/cd包含以下内容
#!/usr/bin/env bash

# Source a .rvmrc file in a directory after changing to it, if it exists.  To
# disable this feature, set rvm_project_rvmrc=0 in /etc/rvmrc or $HOME/.rvmrc
case "${rvm_project_rvmrc:-1}" in
1|cd)
  # cloned from [email protected]:mpapis/bash_zsh_support.git
  source "$rvm_scripts_path/extras/bash_zsh_support/chpwd/function.sh"

  # not using default loading to support older Zsh
  [[ -n "${ZSH_VERSION:-}" ]] &&
  __rvm_version_compare "$ZSH_VERSION" -gt  4.3.4 ||
  {
    function cd()    { __zsh_like_cd cd    "$@" ; }
    function popd()  { __zsh_like_cd popd  "$@" ; }
    function pushd() { __zsh_like_cd pushd "$@" ; }
  }

  __rvm_after_cd()
  {
    \typeset rvm_hook
    rvm_hook="after_cd"
    if [[ -n "${rvm_scripts_path:-}" || -n "${rvm_path:-}" ]]
    then source "${rvm_scripts_path:-$rvm_path/scripts}/hook"
    fi
  }
  __rvm_cd_functions_set()
  {
    __rvm_do_with_env_before
    if [[ -n "${rvm_current_rvmrc:-""}" && "$OLDPWD" == "$PWD" ]]
    then rvm_current_rvmrc=""
    fi
    __rvm_project_rvmrc >&2 || true
    __rvm_after_cd || true
    __rvm_do_with_env_after
    return 0
  }
  [[ " ${chpwd_functions[*]} " == *" __rvm_cd_functions_set "* ]] ||
  chpwd_functions=( "${chpwd_functions[@]}" __rvm_cd_functions_set )

  # This functionality is opt-in by setting rvm_cd_complete_flag=1 in ~/.rvmrc
  # Generic bash cd completion seems to work great for most, so this is only
  # for those that have some issues with that.
  if (( ${rvm_cd_complete_flag:-0} == 1 ))
  then
    # If $CDPATH is set, bash should tab-complete based on directories in those paths,
    # but with the cd function above, the built-in tab-complete ignores $CDPATH. This
    # function returns that functionality.
    _rvm_cd_complete ()
    {
      \typeset directory current matches item index sep
      sep="${IFS}"
      export IFS
      IFS=$'\n'
      COMPREPLY=()
      current="${COMP_WORDS[COMP_CWORD]}"
      if [[ -n "$CDPATH" && ${current:0:1} != "/" ]] ; then
        index=0
        # The change to IFS above means that the \command \tr below should replace ':'
        # with a newline rather than a space. A space would be ignored, breaking
        # TAB completion based on CDPATH again
        for directory in $(printf "%b" "$CDPATH" | \command \tr -s ':' '\n') ; do
          for item in $( compgen -d "$directory/$current" ) ; do
            COMPREPLY[index++]=${item#$directory/}
          done
        done
      else
        COMPREPLY=( $(compgen -d ${current}) )
      fi
      IFS="${sep}";
    }
    complete -o bashdefault -o default -o filenames -o dirnames -o nospace -F _rvm_cd_complete cd
  fi
  ;;
2|prompt)
  if
    [[ -n "${ZSH_VERSION:-}" ]]
  then
    precmd_functions+=(__rvm_do_with_env_before __rvm_project_rvmrc __rvm_do_with_env_after)
  else
    PROMPT_COMMAND="${PROMPT_COMMAND%% }"
    PROMPT_COMMAND="${PROMPT_COMMAND%%;}"
    PROMPT_COMMAND="${PROMPT_COMMAND:-}${PROMPT_COMMAND:+; }__rvm_do_with_env_before; __rvm_project_rvmrc; __rvm_do_with_env_after"
  fi
  ;;
esac
h79rfbju

h79rfbju1#

您可能将RVM用作shell脚本,而不是shell函数。
你可以在一个典型的shell(bash,zsh,.)中这样检查:执行:type rvm
如果显示rvm is /home/ying/.rvm/bin/rvm:您正在用作脚本(在$PATH中找到)
如果显示rvm is a function:作为一个函数使用(更好)。
查看:https://rvm.io/rvm/basics#post-install-configuration
如果您正在使用脚本,并希望将其用作函数:你需要“source”rvm函数,它位于<rvm main folder>/scripts/rvm中,例如如果安装在$HOME中:

source $HOME/.rvm/scripts/rvm
source /usr/local/rvm/scripts/rvm

通常,在RVM安装时,它会在.profile的等价物中添加以下行(取决于shell及其全局或用户):

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

RVM会自动更改版本,如下所述:
https://rvm.io/workflow/projects
对于.ruby-version的检测,需要以下内容:

  • RVM必须是支持该功能的最新版本
  • RVM必须加载到shell中(通常通过.profile或等效的方式),以便作为函数执行
  • shell必须与此回调特性兼容(bash和zsh是)

以下是发生的情况:

  • 当您将rvm作为函数加载时,它会在shell中注册callback
  • 当你cd进入项目时,RVM回调(在shell中)检测到文件.ruby-version(或其他文件),并自动执行相当于rvm use的操作。

例如,我使用zsh(在osx上),它有preexecprecmd回调,它检测ruby版本文件,并在我将cd放入它或子文件夹时应用。它也适用于bash。
如果你很好奇或者想知道为什么它对你不起作用,请查看文件<rvm main dir>/scripts/cd
通常,shell变量chpwd_functions设置为__rvm_cd_functions_set,这是rvmcd之后调用的函数

d7v8vwbk

d7v8vwbk2#

最后厌倦了自己遇到同样的问题,并解决了这个问题。你所要做的就是运行这个由RVM注入的函数:
__rvm_project_rvmrc
我把这个代码段添加到我的.bashrc中,它似乎可以工作:

if [ -f $PWD/.ruby-version ]; then
    __rvm_project_rvmrc
fi

相关问题