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.
(Warning: read @Peter Wagenet's comment ) I like this, most people use rubygems, its reliable, is cross platform
irb(main):001:0> Gem::Platform.local
=> #<Gem::Platform:0x151ea14 @cpu="x86", @os="mingw32", @version=nil>
irb(main):002:0> Gem::Platform.local.os
=> "mingw32"
update use in conjunction with "Update! Addition! Rubygems nowadays..." to mitigate when Gem::Platform.local.os == 'java'
Either
irb(main):002:0> require 'rbconfig'
=> true
irb(main):003:0> Config::CONFIG["arch"]
=> "i686-linux"
or
irb(main):004:0> RUBY_PLATFORM
=> "i686-linux"
I have a second answer, to add more options to the fray. The os rubygem, and their github page has a related projects list.
require 'os' >> OS.windows? => true # or OS.doze? >> OS.bits => 32 >> OS.java? => true # if you're running in jruby. Also OS.jruby? >> OS.ruby_bin => "c:\ruby18\bin\ruby.exe" # or "/usr/local/bin/ruby" or what not >> OS.posix? => false # true for linux, os x, cygwin >> OS.mac? # or OS.osx? or OS.x? => false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With