I want my Ruby program to do different things on a Mac than on a Windows Pc. How can I find out on which system my program is running?
+2
A:
Try reading the PLATFORM
constant:
irb(main):001:0> PLATFORM
=> "i386-mswin32"
Romulo A. Ceccon
2008-10-04 20:41:04
+5
A:
Either
irb(main):002:0> require 'rbconfig'
=> true
irb(main):003:0> Config::CONFIG["arch"]
=> "i686-linux"
or
irb(main):004:0> PLATFORM
=> "i686-linux"
Vinko Vrsalovic
2008-10-04 20:41:57
Don't you mean `Config::CONFIG[‘host_os’]`?
Andrew Grimm
2010-04-23 13:45:27
+8
A:
I use the RUBY_PLATFORM constant, and wrap it in a module.
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
end
It is not perfect, but works well for the platforms that I do development on, and easy enough to extend.
Aaron Hinni
2008-10-04 21:17:49
A:
Try the Launchy gem (gem install launchy
):
require 'rubygems'
require 'launchy'
Launchy::Application.my_os_family # => :windows, :darwin, :nix, or :cygwin
Ryan McGeary
2008-10-05 00:18:39