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

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.

Leave a Comment