Determing what os is running

I need to execute commands differently based on the underlying
platform. I don’t need it down to the patch level or anything like
that – just basic stuff.

How do I test whether I’m runnnig on Windows, Linux, or Mac?

Thanks in advance.

CB

RUBY_PLATFORM.downcase =~ /win32/
will tell you if it is a windows, don’t know about the others though.

James

cb wrote:

I need to execute commands differently based on the underlying
platform. I don’t need it down to the patch level or anything like
that – just basic stuff.

How do I test whether I’m runnnig on Windows, Linux, or Mac?

Thanks in advance.

CB

if RUBY_PLATFORM =~ /linux/ then
#Linux Stuff
elsif RUBY_PLATFORM =~ /mswin32/ then
#Windows Stuff
end

etc.

import os…
wait nvm >.<

On Apr 25, 10:59 am, Kyle H. [email protected] wrote:

if RUBY_PLATFORM =~ /linux/ then
#Linux Stuff
elsif RUBY_PLATFORM =~ /mswin32/ then
#Windows Stuff
end

With other Ruby implementations out there this is no longer wise
because Sun and Microsoft have hija^H^H^H^H co-opted the term
‘platform’. If you check RUBY_PLATFORM with JRuby, for example, you’ll
get ‘java’.

If you need to check the underlying OS, use rbconfig.

require ‘rbconfig’
include Config

case CONFIG[‘host_os’]
when /mswin|windows/i
# Windows
when /linux/i
# Linux
when /sunos|solaris/i
# Solaris
else
# Whatever
end

There’s also the sys-uname library if you need more detailed
information.

Regards,

Dan

In some of my work I’m writing code that works with different host OS
systems AND runs on JRuby so I need to know both the RUBY_PLATFORM
and the host OS.

I’d love to know what people get on other Ruby VMs (1.8, 1.9.
Rubinius) and other host OS’s get as output running this script:

require ‘rbconfig’
puts “Ruby I.:”
puts " RUBY_PLATFORM: #{RUBY_PLATFORM}"
puts " RUBY_VERSION: #{RUBY_VERSION}"
puts " Config::CONFIG[‘RUBY_INSTALL_NAME’]:
#{Config::CONFIG[‘RUBY_INSTALL_NAME’]}"
puts " Config::CONFIG[‘host_os’]: #{Config::CONFIG[‘host_os’]}"

Here’s what I get on MacOS 10.5.2 using the Apple version of Ruby:

Ruby I.:
RUBY_PLATFORM: universal-darwin9.0
RUBY_VERSION: 1.8.6
Config::CONFIG[‘RUBY_INSTALL_NAME’]: ruby
Config::CONFIG[‘host_os’]: darwin9.0

And here’s JRuby version 1.1.1 running on MacOS 10.5.2

Ruby I.:
RUBY_PLATFORM: java
RUBY_VERSION: 1.8.6
Config::CONFIG[‘RUBY_INSTALL_NAME’]: jruby
Config::CONFIG[‘host_os’]: darwin

A subtlety with the JRuby VM is that the Ruby is running on both a
Java OS (in effect) and a host OS and has strong capabilities for
interacting with both systems. This doesn’t usually matter for most
Java programs because they often only deal with the host OS mediated
through Java libraries – but among many other things Ruby is a
systems programming language and Ruby scripts written to support
these tasks often have code written to operate differently on
different host OS platforms. In these cases code written to also run
on JRuby may need to take into account that it is running both in
JRuby on Java AND on which host OS Windows, MacOS, etc.

Hi!

Stephen B. wrote:

#{Config::CONFIG[‘RUBY_INSTALL_NAME’]}"
puts " Config::CONFIG[‘host_os’]: #{Config::CONFIG[‘host_os’]}"

[…]

I’m running Gentoo/Linux on AMD Athlon X2 CPU (32-bit).

ruby a.rb

Ruby I.:
RUBY_PLATFORM: i686-linux
RUBY_VERSION: 1.8.6
Config::CONFIG[‘RUBY_INSTALL_NAME’]: ruby18
Config::CONFIG[‘host_os’]: linux-gnu

ruby19 a.rb

Ruby I.:
RUBY_PLATFORM: i686-linux
RUBY_VERSION: 1.9.0
Config::CONFIG[‘RUBY_INSTALL_NAME’]: ruby19
Config::CONFIG[‘host_os’]: linux-gnu

jruby a.rb

Ruby I.:
RUBY_PLATFORM: java
RUBY_VERSION: 1.8.6
Config::CONFIG[‘RUBY_INSTALL_NAME’]: jruby
Config::CONFIG[‘host_os’]: linux

ruby is ruby 1.8.6 (2008-03-03 patchlevel 114) [i686-linux]
ruby19 is ruby 1.9.0 (2008-04-28 revision 16233) [i686-linux]
svn trunk
jruby is ruby 1.8.6 (2008-04-29 rev 6586) [i386-jruby1.1.1+]
svn trunk

Daniel

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs