Detecting Linux Distribution/Platform in Ruby

What's the best-practice way to find out the platform ruby is running on?

There's always:

begin
require 'win32console'
rescue LoadError
end

I find this easier to write and reason about that trying to decide for myself which OS I'm on and whether or not to load it.

Update: I was thinking win32console was built-in rather than a gem. I believe Win32API is available on all Windows installs, so it's a good proxy to test "Is this Windows?" (rather than "What OS is this, and is that Windows?").

begin
require 'Win32API'
windowsOS = true
rescue LoadError
windowsOS = false
end

if windowsOS
begin
require 'win32console'
rescue LoadError
# Prompt user to install win32console gem
end
end

What is the correct way to detect if ruby is running on Windows?

Preferred Option (Updated based on @John's recommendations):

require 'rbconfig'
is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)

This could also work, but is less reliable (it won't work with much older versions, and the environment variable can be modified)

is_windows = (ENV['OS'] == 'Windows_NT')

(I can't easily test either on all of the rubies listed, or anything but Windows 7, but I know that both will work for 1.9.x, IronRuby, and JRuby).

which in ruby: Checking if program exists in $PATH from ruby

True cross-platform solution, works properly on Windows:

# Cross-platform way of finding an executable in the $PATH.
#
# which('ruby') #=> /usr/bin/ruby
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
end
end
nil
end

This doesn't use host OS sniffing, and respects $PATHEXT which lists valid file extensions for executables on Windows.

Shelling out to which works on many systems but not all.

Distributing Ruby along with application?

Yes, but it's probably prohibitively complicated. Ruby needs to be compiled for the platform you're running it on, so you would need to have access to all the different platforms you'd be distributing to and then compile against those, include all of them in your distribution, and detect at runtime the current platform and pick the correct binary. This whole process is excessive and error-prone. Further, these binaries are not necessarily very small, which will bloat your distributable.

So why not just point them to RubyInstaller for Windows, tell them they're set to go on OS X, and Ubuntu users are generally savvy enough that you can give them more complicated instructions, or, better yet, distribute your application as an Ubuntu package so Ruby gets installed as a dependency.

Cross-platform way to Check if a (non-child) Process is Running

The easiest way is to test which operation system your ruby is running, and then through the command line use a simple test on linux or windows to check if your program is running.

The easiest way often is not the best way. Yet, be prepared to face some strange code, for example, how to simple kill a process on windows. It is your call.

I would love someone say I'm wrong and that there is a great way to manage processes in ruby being cross-platform. Please do.



Related Topics



Leave a reply



Submit