我需要在运行脚本之前测试正确的 Ruby 版本。在项目中,我们有文件 .ruby_version,其中包含所需版本的数量。例如 .ruby_version 文件有:
2.5.5
我可以使用 ruby --version
在用户中获取 Ruby 的当前用户版本,但此命令返回我需要的更多信息。例如:
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-darwin19]
所以我的支票不起作用:
required_ruby=$(cat .ruby-version)
current_ruby=`ruby --version`
if [ "$required_ruby" != "$current_ruby" ]; then
echo "This project uses Ruby version $required_ruby, try to checkout or install it"
fi
知道如何解决吗?
最佳答案
在 Ruby 解释器中有两个特殊常量 RUBY_VERSION
和 RUBY_PATCH_LEVEL
可用,所以你可以调用 ruby
命令并让它给你需要的信息。
$ /usr/bin/ruby -e 'puts RUBY_VERSION'
2.5.5
关于ruby - 我怎样才能找出安装的 ruby 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65456861/