How can I find which operating system my Ruby program is running on?

Huluk picture Huluk · Oct 4, 2008 · Viewed 35.7k times · Source

I want my Ruby program to do different things on a Mac than on Windows. How can I find out on which system my program is running?

Answer

Aaron Hinni picture Aaron Hinni · Oct 4, 2008

Use the RUBY_PLATFORM constant, and optionally wrap it in a module to make it more friendly:

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

It is not perfect, but works well for the platforms that I do development on, and it's easy enough to extend.