One-liner for checking platform?

I want to release some code without encouraging an OS X monoculture:

…but I have some nifty but not absolutely essential functionality
which currently I only know how to do on OS X. Is there a dependable
“if platform?(apple)” I can use?

Actually, now that I put it that way, I’m going to write one. But does
anyone know the quickest, cleanest way?

class Kernel
def os_x?
# ???
end
end

The gems discussion recently had somebody who specified the wrong
string indicating Windows for their platform - I wasn’t sure if that
was the poster’s mistake or a weird overabundance of useful
platform-indicative strings in gems or Ruby, but I want to avoid that
same pitfall.


Giles B.

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

Use the constant RUBY_PLATFORM

On my platform it is

=> “i686-linux”

on windows, its different

on apple i guess too :slight_smile:

if you dont need a one liner you can use this

def which_platform?
$RUBY_PLATFORM ||=
case RUBY_PLATFORM.downcase
when /linux|bsd|solaris|hpux|powerpc-darwin/
:unix
when /mswin32|mingw32|bccwin32/
:windows
when /cygwin/
:cygwin
when /java/
:java
else
:other
end
end

On Tue, Nov 27, 2007 at 03:20:54AM +0900, Giles B. wrote:

Actually, now that I put it that way, I’m going to write one. But does
anyone know the quickest, cleanest way?

class Kernel
def os_x?
# ???
end
end

http://rubyforge.org/projects/platform/

marcel

Hi Giles, my bet is

RUBY_PLATFORM.include?(‘darwin’)

That’s why include?(‘win’) is not a good test for Windows.

Also cygwin.


Giles B.

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

On Nov 26, 2007, at 7:20 PM, Giles B. wrote:

Actually, now that I put it that way, I’m going to write one. But does
anyone know the quickest, cleanest way?

class Kernel
def os_x?

???

end
end

Hi Giles, my bet is

RUBY_PLATFORM.include?(‘darwin’)

That’s why include?(‘win’) is not a good test for Windows.

– fxn

http://rubyforge.org/projects/platform/

Cheers Marcel!


Giles B.

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com