Forum: Ruby detect if windows or linux

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C76a52a7216844fe0e462e3d39ac97e4?d=identicon&s=25 Fleck (Guest)
on 2005-11-14 08:08
(Received via mailing list)
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.
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 dave (Guest)
on 2005-11-14 08:23
(Received via mailing list)
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
428f96cc689eb7419bba3a8bbfcc222a?d=identicon&s=25 stefan (Guest)
on 2005-11-14 08:23
(Received via mailing list)
Alexander Fleck wrote:
> 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'.
Dce0999389d102f9a313af625375304c?d=identicon&s=25 dooby (Guest)
on 2005-11-14 14:58
(Received via mailing list)
Alexander Fleck wrote:
> 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
This topic is locked and can not be replied to.