What Is the Correct Way to Detect If Ruby Is Running on Windows

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).

Use Ruby to detect if platform responds to CLI command

The stdlib mkmf module has a little known helper function, find_executable() which should give you what you want

require 'mkmf'
path = find_executable('cli-command')

I haven't personally tested it on Windows, but I would assume it should work...
http://ruby-doc.org/stdlib-2.3.0/libdoc/mkmf/rdoc/MakeMakefile.html#method-i-find_executable

wrong version of ruby recognized on windows

Install the latest version of rails using the install rails website.

Then you want to type:

$rvm use ruby-2.3.1

This will force your system to use that specific version of ruby. If rails throws some errors, try this:

$gem update rails --no-ri --no-rdoc

side note I know your question is about ruby, but the rails installer should handle ruby too, and I see that your question is still externally in regards to rails.

How to use Ruby to detect whether on Windows Platform, the SHIFT or ALT key is being pressed?

On Win32, how to detect whether a Left Shift or Right ALT is pressed using Perl, Python, or Ruby (or C)? has some clues...

ruby ffi might help, too

Ruby How to determine execution environment

I've used the gem 'OS' which can be found here: http://rubygems.org/gems/os

I haven't tried it with cygwin, but I did replace my own use of the ENV variable hash with it.

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

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.



Related Topics



Leave a reply



Submit