Programing

내 Ruby 프로그램이 실행중인 운영 체제를 어떻게 찾을 수 있습니까?

crosscheck 2020. 9. 24. 07:30
반응형

내 Ruby 프로그램이 실행중인 운영 체제를 어떻게 찾을 수 있습니까?


내 Ruby 프로그램이 Windows와 Mac에서 다른 작업을 수행하기를 원합니다. 내 프로그램이 어떤 시스템에서 실행되고 있는지 어떻게 알 수 있습니까?


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

완벽하지는 않지만 내가 개발하는 플랫폼에서 잘 작동하며 확장하기가 쉽습니다.


(경고 : @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"

업데이트 와 함께 사용하는 "업데이트! 추가! 젬 요즘 ..." 때 완화하는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 rubygemgithub 페이지 에는 관련 프로젝트 목록이 있습니다.

'os'필요

>> OS.windows?
=> true # 또는 OS.doze?

>> OS.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의 경우 참

>> 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

최신 정보! 덧셈! Rubygems는 요즘 Gem.win_platform?.

명확성을 위해 Rubygems 저장소 와이 저장소의 사용 예 :

def self.ant_script
  Gem.win_platform? ? 'ant.bat' : 'ant'
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

For something readily accessible in most Ruby installations that is already somewhat processed for you, I recommend these:

  1. Gem::Platform.local.os #=> eg. "mingw32", "java", "linux", "cygwin", "aix", "dalvik" (code)
  2. Gem.win_platform? #=> eg. true, false (code)

Both these and every other platform checking script I know is based on interpreting these underlying variables:

  1. RbConfig::CONFIG["host_os"] #=> eg. "linux-gnu" (code 1, 2)
  2. RbConfig::CONFIG["arch"] #=> eg. "i686-linux", "i386-linux-gnu" (passed as parameter when the Ruby interpreter is compiled)
  3. RUBY_PLATFORM #=> eg. "i386-linux-gnu", "darwin" - Note that this returns "java" in JRuby! (code)
    • These are all Windows variants: /cygwin|mswin|mingw|bccwin|wince|emx/
  4. RUBY_ENGINE #=> eg. "ruby", "jruby"

Libraries are available if you don't mind the dependency and want something a little more user-friendly. Specifically, OS offers methods like OS.mac? or OS.posix?. Platform can distinguish well between a variety of Unix platforms. Platform::IMPL will return, eg. :linux, :freebsd, :netbsd, :hpux. sys-uname and sysinfo are similar. utilinfo is extremely basic, and will fail on any systems beyond Windows, Mac, and Linux.

If you want more advanced libraries with specific system details, like different Linux distributions, see my answer for Detecting Linux distribution in Ruby.


When I just need to know if it is a Windows or Unix-like OS it is often enough to

is_unix = is_win = false
File::SEPARATOR == '/' ? is_unix = true : is_win = true

참고URL : https://stackoverflow.com/questions/170956/how-can-i-find-which-operating-system-my-ruby-program-is-running-on

반응형