# Detect if windows or linux

**URL:** <https://www.ruby-forum.com/t/detect-if-windows-or-linux/47830>\
**Category:** Ruby\
**Created:** [November 14, 2005, 8:08am UTC](https://www.ruby-forum.com/t/detect-if-windows-or-linux/47830 "2005-11-14T08:08:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fleck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@Fleck](https://www.ruby-forum.com/u/Fleck)\
**Post date:** [November 14, 2005, 8:08am UTC](https://www.ruby-forum.com/t/detect-if-windows-or-linux/47830/1 "2005-11-14T08:08:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Fleck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@Fleck](https://www.ruby-forum.com/u/Fleck)\
**Post date:** [November 14, 2005, 8:23am UTC](https://www.ruby-forum.com/t/detect-if-windows-or-linux/47830/2 "2005-11-14T08:23:39Z")

</div>

Alexander F. 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

---

<div class="post-metadata">

**Author:** ![Fleck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@Fleck](https://www.ruby-forum.com/u/Fleck)\
**Post date:** [November 14, 2005, 8:23am UTC](https://www.ruby-forum.com/t/detect-if-windows-or-linux/47830/3 "2005-11-14T08:23:39Z")

</div>

Alexander F. 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’.

---

<div class="post-metadata">

**Author:** ![Fleck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@Fleck](https://www.ruby-forum.com/u/Fleck)\
**Post date:** [November 14, 2005, 2:58pm UTC](https://www.ruby-forum.com/t/detect-if-windows-or-linux/47830/4 "2005-11-14T14:58:57Z")

</div>

Alexander F. 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
