如何找到我的Ruby程序在哪个操作系统上运行? [英] How can I find which operating system my Ruby program is running on?
本文介绍了如何找到我的Ruby程序在哪个操作系统上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的Ruby程序在Mac上和Windows上做不同的事情.如何找出我的程序在哪个系统上运行?
I want my Ruby program to do different things on a Mac than on Windows. How can I find out on which system my program is running?
推荐答案
使用RUBY_PLATFORM
常量,并可以选择将其包装在模块中以使其更友好:
Use the RUBY_PLATFORM
constant, and optionally wrap it in a module to make it more friendly:
module OS
def OS.windows?
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end
def OS.mac?
(/darwin/ =~ RUBY_PLATFORM) != nil
end
def OS.unix?
!OS.windows?
end
def OS.linux?
OS.unix? and not OS.mac?
end
def OS.jruby?
RUBY_ENGINE == 'jruby'
end
end
这不是完美的,但是对于我开发的平台来说效果很好,并且扩展起来也很容易.
It is not perfect, but works well for the platforms that I do development on, and it's easy enough to extend.
这篇关于如何找到我的Ruby程序在哪个操作系统上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文