What is the correct way to detect if ruby is running on Windows?

Preferred Option (Updated based on @John's recommendations):

require 'rbconfig'
is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)

This could also work, but is less reliable (it won't work with much older versions, and the environment variable can be modified)

is_windows = (ENV['OS'] == 'Windows_NT')

(I can't easily test either on all of the rubies listed, or anything but Windows 7, but I know that both will work for 1.9.x, IronRuby, and JRuby).


This works perfectly for me Also etc does not need to be installed, it comes with ruby.

require "etc"
def check_system
  return "windows" if Etc.uname[:sysname] == "Windows_NT"
  return "linux" if Etc.uname[:sysname] == "Linux"
end

It turns out, there's this way:

Gem.win_platform?