Roman
Roman

Reputation: 763

How can I figure out the installed ruby version?

I need to test for the correct Ruby version before running the script. In project we have file .ruby_version that has number of required version. For example .ruby_version file has:

2.5.5

I could get current user version of Ruby in user using ruby --version, but this command return more information that i needed. For example:

ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-darwin19]

So my check doesn't work:

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

Any idea how to fix it?

Upvotes: 2

Views: 1202

Answers (1)

iBug
iBug

Reputation: 37217

There are two special constants RUBY_VERSION and RUBY_PATCH_LEVEL available in the Ruby interpreter, so you can call the ruby command and let it give your desired information.

$ /usr/bin/ruby -e 'puts RUBY_VERSION'
2.5.5

Upvotes: 7

Related Questions