Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Operating Systems in Ruby [duplicate]

Is there a way to detect the operating system in ruby? I am working on developing a sketchup tool that will need to detect Mac vs. Windows.

like image 347
user1546594 Avatar asked Aug 02 '12 19:08

user1546594


3 Answers

You can use the os gem:

gem install os

And then

require 'os'
OS.linux?   #=> true or false
OS.windows? #=> true or false
OS.java?    #=> true or false
OS.bsd?     #=> true or false
OS.mac?     #=> true or false
# and so on.

See: https://github.com/rdp/os

like image 90
debbie Avatar answered Oct 11 '22 12:10

debbie


Here is the best one I have seen recently. It is from selenium. The reason I think it is the best is it uses rbconfig host_os field which has the advantage of working on MRI and JRuby. RUBY_PLATFORM will say 'java' on JRuby regardless of host os it is running on. You will need to mildly tweak this method:

  require 'rbconfig'

  def os
    @os ||= (
      host_os = RbConfig::CONFIG['host_os']
      case host_os
      when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
        :windows
      when /darwin|mac os/
        :macosx
      when /linux/
        :linux
      when /solaris|bsd/
        :unix
      else
        raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
      end
    )
  end
like image 30
Thomas Enebo Avatar answered Oct 11 '22 14:10

Thomas Enebo


You can use

puts RUBY_PLATFORM

irb(main):001:0> RUBY_PLATFORM
=> "i686-linux"

But @Pete is right.

like image 43
InternetSeriousBusiness Avatar answered Oct 11 '22 14:10

InternetSeriousBusiness