Determine ruby version from within Rails

Ruby on-RailsRubyRuby Enterprise-Edition

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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"

Solution 2 - Ruby on-Rails

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"

Solution 3 - Ruby on-Rails

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                                                          

Solution 4 - Ruby on-Rails

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, ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel VandersluisView Question on Stackoverflow
Solution 1 - Ruby on-RailstrondozerView Answer on Stackoverflow
Solution 2 - Ruby on-RailsMark WestlingView Answer on Stackoverflow
Solution 3 - Ruby on-RailsKomodoDaveView Answer on Stackoverflow
Solution 4 - Ruby on-RailsaverellView Answer on Stackoverflow