Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What is the correct way to detect from within Ruby whether the interpreter is running on Windows? "Correct" includes that it works on all major flavors of Ruby, including 1.8.x, 1.9.x, JRuby, Rubinius, and IronRuby.

The currently top ranked Google results for "ruby detect windows" are all incorrect or outdated. For example, one incorrect way to do it is:

RUBY_PLATFORM =~ /mswin/ 

This is incorrect because it fails to detect the mingw version, or JRuby on Windows.

What's the right way?

like image 812
John Avatar asked Feb 02 '11 06:02

John


2 Answers

It turns out, there's this way:

Gem.win_platform? 
like image 158
x-yuri Avatar answered Oct 05 '22 08:10

x-yuri


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

like image 23
Dylan Markow Avatar answered Oct 05 '22 06:10

Dylan Markow