detect if windows or linux

A

Alexander Fleck

Hi,
what' s the best way to detect if I' m on a windows or linux machine. I don' t want to check that by handling an exception. Is there a function in the standard libraries.
Thanks,
Alex.
 
D

Dave Burt

Alexander Fleck asked:
Hi,
what' s the best way to detect if I' m on a windows or linux machine. I
don' t want to check that by handling an exception. Is there a function in
the standard libraries.

Ruby defines these constants, among others:
RUBY_PLATFORM
RUBY_RELEASE_DATE
RUBY_VERSION

C:\>ruby -e "p PLATFORM"
"i386-mswin32"

Cheers,
Dave
 
S

Stefan Mahlitz

Alexander said:
Hi,
what' s the best way to detect if I' m on a windows or linux machine. I don' t want to check that by handling an exception. Is there a function in the standard libraries.
Thanks,
Alex.

There is the constant RUBY_PLATFORM, on my WinXP-box it contains the
string 'i386-mswin32', my debian has 'i386-linux'.
 
D

daz

Alexander said:
Hi,
what' s the best way to detect if I' m on a windows or linux machine.


As a quick and nasty hack for domestic use, consider one of these two
(depending on taste):

NIX = File.exist?('/dev/null') and !File.exist?('/NUL')
p [:NIX, NIX, RUBY_PLATFORM] # [:NIX, false, "i586-bccwin32"]

# -OR-

WIN = File.exist?('/NUL') and !File.exist?('/dev/null')
p [:WIN, WIN, RUBY_PLATFORM] # [:WIN, true, "i586-bccwin32"]


# Maybe both (for some solidity), adding:

NIX != WIN or raise 'Unrecognised platform'

?

daz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,196
Messages
2,571,036
Members
47,631
Latest member
kukuh

Latest Threads

Top