检测ruby是否在Windows上运行的正确方法是什么? [英] What is the correct way to detect if ruby is running on Windows?

查看:110
本文介绍了检测ruby是否在Windows上运行的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Ruby中检测解释器是否在Windows上运行的正确方法是什么? 正确"包括它可以在Ruby的所有主要版本上使用,包括1.8.x,1.9.x,JRuby,Rubinius和IronRuby.

What is the correct way to detect from within Ruby whether the interpreter is running on Windows? "Correct" includes that it works on all major flavors of Ruby, including 1.8.x, 1.9.x, JRuby, Rubinius, and IronRuby.

当前在红宝石检测窗口"中排名最高的Google搜索结果不正确或已过时.例如,一种不正确的方法是:

The currently top ranked Google results for "ruby detect windows" are all incorrect or outdated. For example, one incorrect way to do it is:

RUBY_PLATFORM =~ /mswin/

这是不正确的,因为它无法检测到mingw版本或Windows上的JRuby.

This is incorrect because it fails to detect the mingw version, or JRuby on Windows.

正确的方法是什么?

推荐答案

首选选项(根据@John的建议进行了更新):

require 'rbconfig'
is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)

这也可以工作,但可靠性较低(不适用于许多较旧的版本,并且可以修改环境变量)

This could also work, but is less reliable (it won't work with much older versions, and the environment variable can be modified)

is_windows = (ENV['OS'] == 'Windows_NT')

(我无法轻松地对列出的所有红宝石或Windows 7以外的任何东西进行测试,但我知道两者都适用于1.9.x,IronRuby和JRuby).

(I can't easily test either on all of the rubies listed, or anything but Windows 7, but I know that both will work for 1.9.x, IronRuby, and JRuby).

这篇关于检测ruby是否在Windows上运行的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆