92

我希望我的 Ruby 程序在 Mac 上和在 Windows 上做不同的事情。如何找出我的程序在哪个系统上运行?

4

11 回答 11

151

使用RUBY_PLATFORM常量,并可选择将其包装在模块中以使其更友好:

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

它并不完美,但适用于我进行开发的平台,并且很容易扩展。

于 2008-10-04T21:17:49.323 回答
25

(警告:阅读@Peter Wagenet 的评论)我喜欢这个,大多数人使用 ruby​​gems 它可靠,是跨平台的

irb(main):001:0> Gem::Platform.local
=> #<Gem::Platform:0x151ea14 @cpu="x86", @os="mingw32", @version=nil>
irb(main):002:0> Gem::Platform.local.os
=> "mingw32"

更新“更新!添加!如今的 Rubygems...”结合使用,以减轻何时Gem::Platform.local.os == 'java'

于 2013-10-16T18:29:08.377 回答
17

任何一个

irb(main):002:0> require 'rbconfig'
=> true
irb(main):003:0> Config::CONFIG["arch"]
=> "i686-linux"

或者

irb(main):004:0> RUBY_PLATFORM
=> "i686-linux"
于 2008-10-04T20:41:57.993 回答
11

我有第二个答案,为战斗添加更多选项。 os ruby​​gem和他们的github 页面有一个相关的项目列表。

需要“操作系统”

>> OS.windows?
=> true # 还是 OS.doze?

>> 操作系统.bits
=> 32

>> OS.java?
=> true # 如果你在 jruby 中运行。还有 OS.jruby?

>> OS.ruby_bin
=> "c:\ruby18\bin\ruby.exe" # 或者 "/usr/local/bin/ruby" 或者什么不是

>> OS.posix?
=> false # 对于 linux、os x、cygwin 为 true

>> OS.mac?# 还是 OS.osx?还是 OS.x?
=> 假的
于 2014-01-13T02:28:00.447 回答
7

试试 Launchy gem ( gem install launchy):

require 'launchy'
Launchy::Application.new.host_os_family # => :windows, :darwin, :nix, or :cygwin 
于 2008-10-05T00:18:39.740 回答
4
require 'rbconfig'
include Config

case CONFIG['host_os']
  when /mswin|windows/i
    # Windows
  when /linux|arch/i
    # Linux
  when /sunos|solaris/i
    # Solaris
  when /darwin/i
    #MAC OS X
  else
    # whatever
end
于 2012-10-17T15:22:08.743 回答
4

更新!添加!Rubygems 现在随Gem.win_platform?.

Rubygems repo 中的示例用法,以及这个,为了清楚起见:

def self.ant_script
  Gem.win_platform? ? 'ant.bat' : 'ant'
end
于 2016-12-03T09:31:50.633 回答
3

对于大多数 Ruby 安装中已经为您处理过的易于访问的东西,我推荐这些:

  1. Gem::Platform.local.os#=> 例如。“mingw32”、“java”、“linux”、“cygwin”、“aix”、“dalvik”(代码
  2. Gem.win_platform?#=> 例如。真假(代码

这些以及我所知道的所有其他平台检查脚本都是基于解释这些基础变量:

  1. RbConfig::CONFIG["host_os"]#=> 例如。“linux-gnu” (代码1、2
  2. RbConfig::CONFIG["arch"]#=> 例如。“i686-linux”、“i386-linux-gnu”(编译 Ruby 解释器时作为参数传递)
  3. RUBY_PLATFORM#=> 例如。"i386-linux-gnu", "darwin" -注意这在 JRuby 中返回 "java"!代码
    • 这些都是 Windows 变体:/cygwin|mswin|mingw|bccwin|wince|emx/
  4. RUBY_ENGINE#=> 例如。“红宝石”,“红宝石”

如果您不介意依赖关系并且想要更用户友好的东西,则可以使用库。具体来说,OS提供了类似OS.mac?or的方法OS.posix?Platform可以很好地区分各种 Unix 平台。Platform::IMPL将返回,例如。:linux,:freebsd,:netbsd,:hpux。sys-unamesysinfo类似。utilinfo非常基础,在 Windows、Mac 和 Linux 之外的任何系统上都会失败。

如果您想要具有特定系统详细信息的更高级库,例如不同的 Linux 发行版,请参阅我对Detecting Linux distribution in Ruby的回答。

于 2019-03-14T17:54:25.997 回答
2

到目前为止,我们使用以下代码做得很好

  def self.windows?
    return File.exist? "c:/WINDOWS" if RUBY_PLATFORM == 'java'
    RUBY_PLATFORM =~ /mingw32/ || RUBY_PLATFORM =~ /mswin32/
  end

  def self.linux?
    return File.exist? "/usr" if RUBY_PLATFORM == 'java'
    RUBY_PLATFORM =~ /linux/
  end

  def self.os
    return :linux if self.linux?
    return :windows if self.windows?
    nil
  end
于 2016-01-19T16:09:43.047 回答
0

os在为 IMGKit 加载不同的二进制文件时使用gem

# frozen_string_literal: true
IMGKit.configure do |config|
  if OS.linux? && OS.host_cpu == "x86_64"
    config.wkhtmltoimage =
      Rails.root.join("bin", "wkhtmltoimage-linux-amd64").to_s
  elsif OS.mac? && OS.host_cpu == "x86_64"
    config.wkhtmltoimage =
      Rails.root.join("bin", "wkhtmltoimage-macos-amd64").to_s
  else
    puts OS.report
    abort "You need to add a binary for wkhtmltoimage for your OS and CPU"
  end
end
于 2022-02-11T17:44:49.547 回答
-7

当我只需要知道它是 Windows 还是类 Unix 操作系统时,通常就足够了

is_unix = is_win = false
File::SEPARATOR == '/' ? is_unix = true : is_win = true
于 2013-09-27T09:40:34.843 回答