Determine ruby version from within Rails

独自空忆成欢 提交于 2019-11-29 21:10:45

Use the Top-Level Constant

RUBY_VERSION

other useful Top-Level Constants are

RUBY_PATCHLEVEL
RUBY_PLATFORM
RUBY_RELEASE_DATE

here is an irb session:

irb(main):001:0> RUBY_VERSION
=> "1.8.7"

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"

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                                                          
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!