Detecting Operating Systems in Ruby

28,902

Solution 1

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

Solution 2

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

Solution 3

You can use

puts RUBY_PLATFORM

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

But @Pete is right.

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

Share:
28,902

Related videos on Youtube

user1546594
Author by

user1546594

Updated on July 09, 2022

Comments

  • user1546594
    user1546594 almost 2 years

    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.

    • Pete
      Pete almost 12 years
      Can you give us more details around why you need to do this? Often feature detection can be more helpful than blanket OS detection.
  • max
    max over 9 years
    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.
  • robertodecurnex
    robertodecurnex over 9 years
    RUBY_PLATFORM will return "java" when using JRuby, regardless the OS.
  • Gerry
    Gerry over 9 years
    Thanks for finding that. Awesome answer. :) Sadly, you have two years of votes to catch up on.
  • Gerry
    Gerry over 9 years
    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: stackoverflow.com/a/20579735/109561
  • Mosselman
    Mosselman about 7 years
    This sufficient for something like detecting whether you are on OSX or not.
  • bfontaine
    bfontaine almost 7 years
    @Mosselman It is not. When running JRuby on macOS you’ll always get "java".
  • Mosselman
    Mosselman almost 7 years
    @bfontaine thanks, good to know. How would you detect that in the case of JRuby on macOS?
  • bfontaine
    bfontaine almost 7 years
    @Mosselman You can use RbConfig, see Thomas’ answer
  • utx0_
    utx0_ over 6 years
    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. <3
  • Dorian
    Dorian about 2 years
    gives x86_64-darwin21 on macos so would break after upgrades, the os solution is much nicer