如何找到我的 Ruby 程序在哪个操作系统上运行? [英] How can I find which operating system my Ruby program is running on?

查看:20
本文介绍了如何找到我的 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屋!

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