Determine ruby version from within Rails
Asked Answered
M

4

95

Is there a way to determine what version of Ruby is running from within Rails (either on the web or through script/console)? I have Ruby 1.8.6 installed but I've also installed Ruby Enterprise Edition 1.8.7-20090928 and want to ensure that it's using the right installation.

Myrick answered 19/10, 2009 at 16:34 Comment(0)
H
191

Use this global constant:

RUBY_VERSION

Other relevant global constants include:

RUBY_PATCHLEVEL
RUBY_PLATFORM
RUBY_RELEASE_DATE

Usage example via irb session:

irb(main):001:0> RUBY_VERSION
=> "1.8.7"
Hudspeth answered 19/10, 2009 at 16:41 Comment(0)
D
14

Try the constant RUBY_VERSION. I use this extensively to determine whether I'm running under 1.8 or JRuby.

Also, if you're not in production mode, you can do a quick check by hitting the URL "/rails/info/properties"

Duelist answered 19/10, 2009 at 16:40 Comment(0)
P
14

Use RUBY_VERSION as mentioned by others.

You can then use Gem::Version to do version string comparison:

require 'rubygems' # Only needed for ruby pre-1.9.0 but it's safe for later versions (evaluates to false).
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('1.9.0')
    extend DL::Importable                                    
else                                                         
    extend DL::Importer                                      
end                                                          
Paction answered 2/3, 2015 at 10:25 Comment(2)
I wish this answer was there -- https://mcmap.net/q/225031/-programmatically-getting-full-ruby-version/322020 -- but I can't move it so I ask you to delete this one and post it there.Paintbrush
The mention of Gem::Version was very helpful, good addition.Cooksey
S
11

In addition to the RUBY_VERSION constant and friends you may also want to check out Config::CONFIG. This hash contains not only the version numbers but also a ton of other useful runtime information, like the path to the binary, the hostname, ...

Saito answered 21/10, 2009 at 14:8 Comment(1)
thank you, I use 'puts RbConfig.ruby' for ruby 2.0 and rails 4.0 and it return '/usr/local/rvm/rubies/ruby-2.0.0-p247/bin/ruby'Ingeborgingelbert

© 2022 - 2024 — McMap. All rights reserved.