Module: Platform

Defined in:
lib/platform.rb

Overview

Platform

Platform is a simple module which parses the Ruby constant RUBY_PLATFORM and works out the OS, it’s implementation, and the architecture it’s running on.

The motivation for writing this was coming across a case where

if RUBY_PLATFORM =~ /win/

didn’t behave as expected (i.e. on powerpc-darwin-8.1.0)

It is hoped that providing a library for parsing the platform means that we can cover all the cases and have something which works reliably 99% of the time.

Please report any anomalies or new combinations to the author(s).

Use

require “platform”

defines

Platform::OS (:unix,:win32,:vms,:os2) Platform::IMPL (:macosx,:linux,:mswin) Platform::ARCH (:powerpc,:x86,:alpha)

if an unknown configuration is encountered any (or all) of these constant may have the value :unknown.

To display the combination for your setup run

ruby platform.rb

Constant Summary collapse

PLATFORMS =

Each platform is defined as

/regex/, ::OS, ::IMPL

define them from most to least specific and

/.*/, :unknown, :unknown

should always come last

whither AIX, SOLARIS, and the other unixen?

[
   [ /darwin/i,   :unix,      :macosx ],
   [ /linux/i,    :unix,      :linux ],
   [ /freebsd/i,  :unix,      :freebsd ],
   [ /netbsd/i,   :unix,      :netbsd ],
   [ /mswin/i,    :win32,     :mswin ],
   [ /cygwin/i,   :hybrid,    :cygwin ],
   [ /mingw/i,    :win32,     :mingw ],
   [ /bccwin/i,   :win32,     :bccwin ],
   [ /wince/i,    :win32,     :wince ],
   [ /vms/i,      :vms,       :vms ],
   [ /os2/i,      :os2,       :os2 ],
   [ /solaris/i,  :unix,      :solaris ],
   [ /aix/i,      :unix,      :aix ],
   [ /irix/i,     :unix,      :irix ],
   [ /hpux/i,     :unix,      :hpux ],
   [ /.*/,        :unknown,   :unknown ]
]
ARCHS =

What about AMD, Turion, Motorola, etc..?

[
   [ /i\d86/,        :x86 ],
   [ /x86_64|x64/,   :x86_64],
   [ /ia64/,         :ia64 ],
   [ /powerpc/,      :powerpc ],
   [ /alpha/,        :alpha ],
   [ /sparc/i,       :sparc ],
   [ /mips/i,        :mips ],
   [ /hppa/i,        :parisc ],
   [ /.*/,           :unknown ]
]

Class Method Summary collapse

Class Method Details

.descriptionObject



96
97
98
99
100
101
102
# File 'lib/platform.rb', line 96

def self.description
  {
    :os => OS,
    :impl => IMPL,
    :arch => ARCH
  }
end

.for_platform(os, impl = IMPL, arch = ARCH) ⇒ Object

Execute the given block of code for a specific combination (or combinations) of platform operating system, implementation, and architecture.

e.g. Platform.invoke_if( :unix, :macosx )



88
89
90
91
92
93
94
# File 'lib/platform.rb', line 88

def self.for_platform( os, impl = IMPL, arch = ARCH )
  if block_given?
    if Array( os ).include?( OS ) && Array( impl ).include?( IMPL ) && Array( arch ).include?( ARCH )
      yield( OS, IMPL, ARCH )
    end
  end
end