views:

263

answers:

4

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
+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
Don't you mean `Config::CONFIG[‘host_os’]`?
Andrew Grimm
+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
One case where it won't work is if you're using jruby.
Andrew Grimm
A: 

Try the Launchy gem (gem install launchy):

require 'rubygems'
require 'launchy'
Launchy::Application.my_os_family # => :windows, :darwin, :nix, or :cygwin
Ryan McGeary