How to get a ruby version in a project Ruby on Rails with examples

This short tutorial explains how to find the ruby version installed on a project.

How to find the ruby version in a project?

In Terminal, you can type the ruby --version command if ruby is installed.

C:\Users\Kiran>ruby --version
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x64-mingw32]

To know the ruby version in the interactive ruby terminal(IRB) console.

C:\Users\abc>irb
irb(main):001:0> RUBY_VERSION
=> "2.7.0"
irb(main):002:0>

In Ruby, You can use other global constants like the below in the Ruby on Rails application.

RUBY_PATCHLEVEL RUBY_PLATFORM RUBY_RELEASE_DATE RUBY_DESCRIPTION

Here are an example commands for the Ruby version platform, release date, and complete release version

irb(main):001:0> RUBY_VERSION
=> "2.7.0"
irb(main):002:0> RUBY_PATCHLEVEL
=> 0
irb(main):003:0> RUBY_PLATFORM
=> "x64-mingw32"
irb(main):004:0> RUBY_RELEASE_DATE
=> "2019-12-25"
irb(main):005:0> RUBY_DESCRIPTION
=> "ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x64-mingw32]"
irb(main):006:0>

You can also print the ruby version programmatically using puts to console.

puts RUBY_DESCRIPTION

Output:

ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]

Ruby version in Heroku environment

You can run the ruby -v command with Heroku run

heroku run "ruby -v"

In the Linux environment, You can type which command to know the ruby version and location.

which -a ruby