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
How to "Nest" the Inclusion of Modules When Using the Ruby on Rails Activesupport::Concern Feature
Heroku: Pg::Connectionbad: Could Not Connect to Server: Connection Refused
How to Return a Fixed Length Binary Representation of an Integer in Ruby
How to Use Fiddle to Pass or Return a Struct to Native Code
Compute Geo Distance in Elasticsearch
Implicit Argument Passing of Super from Method Defined by Define_Method() Is Not Supported
Phonegap and Rails 3: How to Interact with a Rails 3 App
Did I Install Ruby 1.9.3 Correctly on Rhel
"The Ruby Way" (Mixins and Class Reopening) VS. Dependency Injection
Elegant Command-Parsing in an Oop-Based Text Game
Ruby/Rails - How to Create a Class and Access It from the Controller
Ssl Error on Http Post (Unknown Protocol)
Cannot Install Ruby Gems - Zlib Error
How to Extend a Class from an Initializer and Have It Reload in Development Environment
How to Parse a Number from a String That May Have a Leading Zero
Postgresql Ilike with Multiple Matches in Rails Activerecord