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.
Asked
Active
Viewed 3.0k times
50
-
4Can you give us more details around *why* you need to do this? Often feature detection can be more helpful than blanket OS detection. – Pete Aug 02 '12 at 19:05
4 Answers
76
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.
-
2Thanks for finding that. Awesome answer. :) Sadly, you have two years of votes to catch up on. – Gerry Jan 22 '15 at 22:41
60
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

Thomas Enebo
- 822
- 6
- 3
-
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: http://stackoverflow.com/a/20579735/109561 – Gerry Jan 22 '15 at 22:45
-
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 – utx0_ Oct 19 '17 at 08:07
29
You can use
puts RUBY_PLATFORM
irb(main):001:0> RUBY_PLATFORM
=> "i686-linux"
But @Pete is right.

InternetSeriousBusiness
- 2,605
- 16
- 17
-
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. – max Nov 26 '14 at 06:06
-
1RUBY_PLATFORM will return "java" when using JRuby, regardless the OS. – Roberto Decurnex Dec 03 '14 at 18:53
-
This sufficient for something like detecting whether you are on OSX or not. – Mosselman Feb 27 '17 at 15:47
-
@Mosselman It is not. When running JRuby on macOS you’ll *always* get `"java"`. – bfontaine Jul 18 '17 at 21:29
-
@bfontaine thanks, good to know. How would you detect that in the case of JRuby on macOS? – Mosselman Jul 19 '17 at 22:01
-
@Mosselman You can use `RbConfig`, see [Thomas’ answer](https://stackoverflow.com/a/13586108/735926) – bfontaine Jul 20 '17 at 13:38
-
gives `x86_64-darwin21` on macos so would break after upgrades, the os solution is much nicer – Dorian Feb 11 '22 at 17:37
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.

Todd A. Jacobs
- 81,402
- 15
- 141
- 199