【发布时间】:2010-09-15 07:38:03
【问题描述】:
我希望我的 Ruby 程序在 Mac 上和在 Windows 上做不同的事情。如何找出我的程序在哪个系统上运行?
【问题讨论】:
标签: ruby operating-system
我希望我的 Ruby 程序在 Mac 上和在 Windows 上做不同的事情。如何找出我的程序在哪个系统上运行?
【问题讨论】:
标签: ruby operating-system
使用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
它并不完美,但适用于我进行开发的平台,并且很容易扩展。
【讨论】:
RbConfig::CONFIG["host_os"] 来获取你的操作系统。
(警告:阅读@Peter Wagenet 的评论) 我喜欢这个,大多数人使用rubygems,它可靠,是跨平台的
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"
更新与"Update! Addition! Rubygems nowadays..."结合使用以减轻Gem::Platform.local.os == 'java'时的影响
【讨论】:
要么
irb(main):002:0> require 'rbconfig'
=> true
irb(main):003:0> Config::CONFIG["arch"]
=> "i686-linux"
或
irb(main):004:0> RUBY_PLATFORM
=> "i686-linux"
【讨论】:
Config::CONFIG[‘host_os’]吗?
RbConfig::CONFIG["arch"]
irb(main):002:0> require 'rbconfig' => false
(irb):10:in irb_binding':使用 RbConfig 而不是过时和不推荐使用的 Config。所以我使用了它,RbConfig::CONFIG.each 并列出了所有不同的值。也许你可以在那里找到一些东西来帮助你找到你要找的东西。
我有第二个答案,可以添加更多选项。 The os rubygem,他们的github page有相关项目列表。
需要“操作系统” >> 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? => 假的【讨论】:
试试 Launchy gem (gem install launchy):
require 'launchy'
Launchy::Application.new.host_os_family # => :windows, :darwin, :nix, or :cygwin
【讨论】:
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
【讨论】:
case Config::CONFIG['host_os'] ?
RbConfig::Obsolete::CONFIG['host_os'] ...+不需要包含Config
include 或两种类型的模块,然后这是 IMO 的最佳答案。注意他是如何include'd 模块的,所以不需要 RbConfig 或 Config。
更新!另外! Rubygems 现在附带Gem.win_platform?。
Example usages in the Rubygems repo,还有这个,为了清楚起见:
def self.ant_script
Gem.win_platform? ? 'ant.bat' : 'ant'
end
【讨论】:
对于在大多数 Ruby 安装中易于访问且已经为您处理过的东西,我推荐这些:
Gem::Platform.local.os #=> 例如。 “mingw32”、“java”、“linux”、“cygwin”、“aix”、“dalvik”(code)Gem.win_platform? #=> 例如。对,错 (code)这些以及我所知道的所有其他平台检查脚本都是基于解释这些底层变量:
RbConfig::CONFIG["host_os"] #=> 例如。 “linux-gnu”(代码1,2)RbConfig::CONFIG["arch"] #=> 例如。 "i686-linux", "i386-linux-gnu" (传递为parameter when the Ruby interpreter is compiled)RUBY_PLATFORM #=> 例如。 "i386-linux-gnu", "darwin" - 注意这会在 JRuby 中返回 "java"! (code)
/cygwin|mswin|mingw|bccwin|wince|emx/
RUBY_ENGINE #=> 例如。 "红宝石", "jruby"如果您不介意依赖关系并且想要一些对用户更友好的东西,则可以使用库。具体来说,OS 提供了OS.mac? 或OS.posix? 等方法。 Platform可以很好的区分各种Unix平台。 Platform::IMPL 将返回,例如。 :linux,:freebsd,:netbsd,:hpux。 sys-uname 和 sysinfo 类似。 utilinfo 非常基础,在 Windows、Mac 和 Linux 以外的任何系统上都会失败。
如果您想要具有特定系统详细信息的更高级库,例如不同的 Linux 发行版,请参阅我对 Detecting Linux distribution in Ruby 的回答。
【讨论】:
RUBY_PLATFORM 或RbConfig::CONFIG["host_os"] 的填充方式。看代码,我还是不太清楚。
到目前为止,我们使用以下代码做得很好
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
【讨论】:
当我只需要知道它是 Windows 还是类 Unix 操作系统时,通常就足够了
is_unix = is_win = false
File::SEPARATOR == '/' ? is_unix = true : is_win = true
【讨论】:
File::SEPARATOR。最好像在 Ruby 中开发一样对平台进行鸭式输入。但是,如果您必须知道平台是否是 Windows,请提出问题而不是试图推断它。