[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

determing what os is running

Note Myself

4/25/2008 4:37:00 PM

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
7 Answers

James Bracy

4/25/2008 4:58:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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

James

On Fri, Apr 25, 2008 at 11:40 AM, cb <example@example.com> 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
>
>

Kyle Hunter

4/25/2008 4:59:00 PM

0

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.
--
Posted via http://www.ruby-....

Daniel Berger

4/25/2008 5:54:00 PM

0



On Apr 25, 10:59 am, Kyle Hunter <keletmas...@gmail.com> wrote:
> 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

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

Zachary Scott

4/26/2008 12:46:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

import os..
wait nvm >.<

On Fri, Apr 25, 2008 at 1:53 PM, Daniel Berger <djberg96@gmail.com> wrote:

>
>
> On Apr 25, 10:59 am, Kyle Hunter <keletmas...@gmail.com> wrote:
> > 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
>
> 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
>
>

Stephen Bannasch

4/29/2008 3:42:00 PM

0

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 Info:"
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 Info:
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 Info:
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.

Daniel Schömer

4/29/2008 7:03:00 PM

0

Hi!

Stephen Bannasch wrote:
> [...]
>
> 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 Info:"
> 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']}"
>
> [...]

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

# ruby a.rb
Ruby Info:
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 Info:
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 Info:
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

Denis Defreyne

4/29/2008 8:02:00 PM

0