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
How do I ask Ruby what system it's running on?
You can use the RUBY_PLATFORM
constant to inquire:
>> RUBY_PLATFORM
=> "i686-linux"
For this purpose we have used code similar to https://github.com/kotp/rlcw/blob/master/lib/platform.rb:
module Platform
def check_operating_system
# The Mac check has to be proceed before the Win check!
# Perhaps checking for /mswin/ will reduce this requirement.
case RUBY_PLATFORM
when /ix/i, /ux/i, /gnu/i, /sysv/i, /solaris/i, /sunos/i, /bsd/i
require 'gtk2'
Gtk.init
@clip = Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD)
:unix
when /darwin/i
:mac_os_x
when /mswin/i, /ming/i
require 'vr/clipboard'
@clip = Clipboard.new(2048)
:windows
else
:other
end
end
end
Checking if a program is running
Here's a contrived example that uses tasklist
. It assigns the result of the backtick system call to a status
variable. If this variable is empty, then the specified program isn't running.
def find_process
status = `tasklist | find "notepad.exe"`
if status.empty?
puts "Process is not running"
else
puts "Process is running"
end
end
find_process
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).
How can I get the current operating system name in Elixir?
You can call Erlang os:type
to get platform name info:
type() -> {Osfamily, Osname}
Types:
Osfamily = unix | win32
Osname = atom()
Returns the Osfamily and, in some cases, Osname of the current
operating system.On Unix, Osname will have same value as
uname -s
returns, but in
lower case. For example, on Solaris 1 and 2, it will be sunos.In Windows, Osname will be either
nt
(on Windows NT), orwindows
(on Windows 95).
In Elixir you probably have to call
:os.type()
to refer to that function with Osfamily
being :unix
or :win32
.
Related Topics
Best Way to Create Custom Config Options For My Rails App
How to Split (Chunk) a Ruby Array into Parts of X Elements
How to Use Ruby to Colorize the Text Output to a Terminal
Best Way to Convert Strings to Symbols in Hash
Regex to Split Bbcode into Pieces
Difference Between Map, Each, and Collect
How to Pass Arguments into a Rake Task With Environment in Rails
How to Reload the Current Page in Ruby on Rails
Does Ruby Have Real Multithreading
How to Parse Json With Ruby on Rails
How to Avoid Running Activerecord Callbacks
How to Break Out from a Ruby Block
Where Is Ruby'S String Literal Juxtaposition Feature Officially Documented