I want my Ruby program to do different things on a Mac than on Windows. How can I find out on which system my program is running?
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.