Detecting Operating Systems in Ruby [duplicate]
Asked Answered
G

4

50

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.

Gottschalk answered 2/8, 2012 at 19:4 Comment(1)
Can you give us more details around why you need to do this? Often feature detection can be more helpful than blanket OS detection.Dignify
R
77

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

Rosariorosarium answered 14/12, 2013 at 4:38 Comment(1)
Thanks for finding that. Awesome answer. :) Sadly, you have two years of votes to catch up on.Groundage
E
60

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
Eulogistic answered 27/11, 2012 at 14:13 Comment(3)
Nice, but I think you should update your answer to make note of the "os" gem, which already addresses the JRuby problem you mentioned and gets this detection code of our your code base. See: https://mcmap.net/q/219556/-detecting-operating-systems-in-ruby-duplicateGroundage
This is also a great method if you cant install a gem to a system. Like in the case I am currently working on, where I am building a low level system script that doesn't have access to install anything at the point where I need to know the os version. <3Cephalic
@utx0_: Note that the only dependencies for the os library are YAML and RbConfig, which are both part of the Ruby standard library. Forget packaging systems, just download and ship a copy of os.rb with your script.Jasper
C
29

You can use

puts RUBY_PLATFORM

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

But @Pete is right.

Cortezcortical answered 2/8, 2012 at 19:7 Comment(7)
just wanted to let people know that if you're running a 32 bit ruby on a 64 bit windows, RUBY_PLATFORM will show you that architecture is 32 bit.Shorten
RUBY_PLATFORM will return "java" when using JRuby, regardless the OS.Hutson
This sufficient for something like detecting whether you are on OSX or not.Monto
@Monto It is not. When running JRuby on macOS you’ll always get "java".Samaria
@Samaria thanks, good to know. How would you detect that in the case of JRuby on macOS?Monto
@Monto You can use RbConfig, see Thomas’ answerSamaria
gives x86_64-darwin21 on macos so would break after upgrades, the os solution is much nicerKearse
B
4

You can inspect the RUBY_PLATFORM constant, but this is known to be unreliable in certain cases, such as when running JRuby. Other options include capturing the output of the uname -a command on POSIX systems, or using a detection gem such as sys-uname.

Bergwall answered 2/8, 2012 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.