从 Rails 中确定 ruby​​ 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1589751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:52:37  来源:igfitidea点击:

Determine ruby version from within Rails

ruby-on-railsrubyruby-enterprise-edition

提问by Daniel Vandersluis

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.

有没有办法确定从 Rails 内部(在 Web 上或通过script/console)运行的 Ruby 版本?我安装了 Ruby 1.8.6,但我也安装了 Ruby Enterprise Edition 1.8.7-20090928,并希望确保它使用正确的安装。

回答by avguchenko

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 会议:

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

回答by Mark Westling

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

试试常量 RUBY_VERSION。我广泛使用它来确定我是在 1.8 还是 JRuby 下运行。

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

此外,如果您未处于生产模式,则可以通过点击 URL“/rails/info/properties”进行快速检查

回答by averell

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

除了 RUBY_VERSION 常量和朋友,您可能还想查看 Config::CONFIG。该散列不仅包含版本号,还包含大量其他有用的运行时信息,例如二进制文件的路径、主机名……

回答by KomodoDave

Use RUBY_VERSIONas mentioned by others.

使用RUBY_VERSION其他人提到的。

You can then use Gem::Versionto do version string comparison:

然后您可以使用Gem::Version进行版本字符串比较:

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