从 Rails 内部确定 ruby 版本

有没有一种方法可以确定哪个版本的 Ruby 是在 Rails 中运行的(无论是在 Web 上还是通过 script/console) ?我已经安装了 Ruby1.8.6,但我也安装了 RubyEnterpriseEdition 1.8.7-20090928,并希望确保它使用了正确的安装。

39426 次浏览

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"

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"

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

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