我希望我的 Ruby 程序在 Mac 上执行与 Windows 上不同的操作。我怎样才能知道我的程序正在哪个系统上运行?
使用
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
它并不完美,但对于我进行开发的平台来说效果很好,并且很容易扩展。
我喜欢这个,大多数人都用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 与 “更新!添加!现在的 Rubygems...” 结合使用,以缓解
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"
我有第二个答案,为冲突添加更多选项。 os rubygem,以及他们的 github 页面 有相关项目列表。
需要“操作系统” >> 操作系统.windows? => true # 还是 OS.doze? >> 操作系统位 => 32 >> 操作系统.java? => true # 如果你在 jruby 中运行。还有OS.jruby? >> 操作系统.ruby_bin =>“c: uby18 在 uby.exe" # 或 "/usr/local/bin/ruby" 或者其他什么 >> 操作系统.posix? => false # 对于 linux、os x、cygwin 为 true >> 操作系统.mac? # 还是 OS.osx?还是OS.x? => 假
尝试 Launchy gem(
gem install launchy
):
require 'launchy'
Launchy::Application.new.host_os_family # => :windows, :darwin, :nix, or :cygwin
Gem.win_platform?
。
Rubygems 存储库中的示例用法,为了清楚起见,还有这个:
def self.ant_script
Gem.win_platform? ? 'ant.bat' : 'ant'
end
OS gem 是最用户友好的库,提供像
OS.mac?
或 OS.posix?
这样的方法。它很小,仅依赖于 Ruby 标准库。
对于大多数 Ruby 安装中已经为您处理过的易于访问的内容,请尝试以下操作:
Gem.win_platform?
#=> 例如。真,假(代码)Gem::Platform.local.os
#=> 例如。 “mingw32”,“java”,“linux”,“cygwin”,“aix”,“dalvik”(代码)这些以及我所知道的所有其他平台检查脚本都是基于解释这些底层变量:
RbConfig::CONFIG["host_os"]
#=> 例如。 “linux-gnu”(代码1,2)RbConfig::CONFIG["arch"]
#=> 例如。 “i686-linux”、“i386-linux-gnu”(编译 Ruby 解释器时作为参数传递)RUBY_PLATFORM
#=> 例如。 “i386-linux-gnu”、“darwin” - 请注意,这在 JRuby 中返回“java”!(代码)这些都是 Windows 变体:/cygwin|mswin|mingw|bccwin|wince|emx/
RUBY_ENGINE
#=> 例如。 “红宝石”,“红宝石”其他库包括Platform,可以很好地区分各种Unix平台。
Platform::IMPL
将返回,例如。 :Linux,:freebsd,:netbsd,:hpux。 sys-uname 和 sysinfo 类似。 utilinfo 非常基础,在 Windows、Mac 和 Linux 之外的任何系统上都会失败。
如果您想要具有特定系统详细信息的更高级库,例如不同的 Linux 发行版,请参阅我的答案 Detecting Linux distribution in Ruby。
此外,您可能会考虑功能检测,而不是尝试嗅探操作系统。
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
到目前为止,我们使用以下代码做得很好
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
在为 IMGKit 加载不同的二进制文件时使用
os
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
当我只需要知道它是 Windows 还是类 Unix 操作系统时,通常就足够了
is_unix = is_win = false
File::SEPARATOR == '/' ? is_unix = true : is_win = true