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

What is the correct way to detect from within Ruby whether the interpreter is running on Windows? "Correct" includes that it works on all major flavors of Ruby, including 1.8.x, 1.9.x, JRuby, Rubinius, and IronRuby.
The currently top ranked Google results for "ruby detect windows" are all incorrect or outdated. For example, one incorrect way to do it is:
RUBY_PLATFORM =~ /mswin/
This is incorrect because it fails to detect the mingw version, or JRuby on Windows.
What's the right way?

It turns out, there's this way:
Gem.win_platform?

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

This works perfectly for me
Also etc does not need to be installed, it comes with ruby.
require "etc"
def check_system
return "windows" if Etc.uname[:sysname] == "Windows_NT"
return "linux" if Etc.uname[:sysname] == "Linux"
end

(File::ALT_SEPARATOR || File::SEPARATOR) == '\\'

Related

Best Practices for multiple OS consistency when using Ruby's Dir.glob

I noticed recently during a debugging session that Dir.glob (aka Dir[]) behaves differently depending on the OS. Specifically the order the files are returned in is different.
What are recommended ways to use Dir.glob in Ruby when you know the code will be used on a variety of OSes?
Example Difference:
I cloned the project DeckSchrubber in Linux and Windows
Windows:
irb(main):003:0> puts Dir['./*']
./CHANGELOG.md
./LICENSE
./main.go
./README.md
./types.go
./util
=> nil
Linux:
irb(main):011:0> puts Dir['./*']
./main.go
./LICENSE
./util
./types.go
./README.md
./CHANGELOG.md
=> nil
Once again I am asking for solutions and idioms to ensure the output is canonical.
Usually, FS libraries behave in a different way on Mac and Linux. I don't consider windows as a platform for Ruby.
So, from my experience, it was enough just to add a conditional operator, that checks the current platform name, and sorts the result in required way. As far as I remember, the difference was in the order of returned files.

Programatically get current version of ruby standard library

I looking for a way in my ruby program to determine the version of Ruby that is running my program as well as the version of the Standard Libary?
The version of Ruby is stored in the RUBY_VERSION global constant.
puts RUBY_VERSION
You can compare versions by using classes provided by Rubygems:
min_ruby_version = Gem::Requirement.new(">=2.2.0")
current_ruby_version = Gem::Version.new(RUBY_VERSION)
# check if ruby conforms to version req using =~ operator
if min_ruby_version =~ current_ruby_version
do_this
else
do_that
end

Ruby/Buildr―getting user-, host and OS-name

I am looking for an robust, elegant and portable solution to get the user-, host- and osname in Ruby. I would like to create a folder structure which is set up like this:
linux/maschine1/user53, or
linux/maschine2/user53, or
windows/maschine1/user53, or
mac/supermac/superuser
the name of the user- and hostname should reflect the user#computer:~$ on a linux shell and the operating system should be divided in win, linux and mac.
I found several approaches, using the ENV['USER'/'USERNAME'], or Etc.getLogin() for the username, Socket.gethostname for the computername and the RUBY_PLATFORM constant for the os-name. But all of them have issues when running in different Plattforms like JRuby or even on different operating systems.
So which choice would be the best for each?
Thanks!
Edit:
I came to this solution a short moment before the answer was given. Since Buildr can also use Java commands it is possible to get the values like this:
os = Java.java.lang.System.getProperty('os.name')
usr = Java.java.lang.System.getProperty('user.name')
host = Java.java.net.InetAddress.getLocalHost().getHostName()
But I think I am going to use the pure ruby way to keep the language as consistent as possible.
Not sure there is really a gem that does it all, but Etc.getlogin and Socket.getshostname should work across platforms I think. I use this or variations of to get the os:
require 'rbconfig'
def os
#os ||= (
host_os = RbConfig::CONFIG['host_os']
case host_os
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
:windows
when /darwin|mac os/
:macosx
when /linux/
:linux
when /solaris|bsd/
:unix
else
raise "unknown os: #{host_os.inspect}"
end
)
end
which is ripped off from the selenium gem: https://code.google.com/p/selenium/source/browse/rb/lib/selenium/webdriver/common/platform.rb which might provide a bit more inspiration.

Unicode filenames on Windows in Ruby

I have a piece of code that looks like this:
Dir.new(path).each do |entry|
puts entry
end
The problem comes when I have a file named こんにちは世界.txt in the directory that I list.
On a Windows 7 machine I get the output:
???????.txt
From googling around, properly reading this filename on windows seems to be an impossible task. Any suggestions?
I had the same problem & just figured out how to get the entries of a directory in UTF-8 in Windows. The following worked for me (using Ruby 1.9.2p136):
opts = {}
opts[:encoding] = "UTF-8"
entries = Dir.entries(path, opts)
entries.each do |entry|
# example
stat = File::stat(entry)
puts "Size: " + String(stat.size)
end
You're out of luck with pure ruby (either 1.8 or 1.9.1) since it uses the ANSI versions of the Windows API.
It seems like Ruby 1.9.2 will support Unicode filenames on Windows. This bug report has 1.9.2 as target. According to this announcement Ruby 1.9.2 will be released at the end of July 2010.
If you really need it earlier you could try to use FindFirstFileW etc. directly via Win32API.new or win32-api.
My solution was to use Dir.glob instead of Dir.entries. But it only works with * parameter. It does not work when passing a path (c:/dir/*). Tested in 1.9.2p290 and 1.9.3p0 on Windows 7.
There are many other issues with unicode paths on Windows. It is still an open issue. The patches are currently targeted at Ruby 2.0, which is rumored to be released in 2013.

Open the default browser in Ruby

In Python, you can do this:
import webbrowser
webbrowser.open_new("http://example.com/")
It will open the passed in url in the default browser
Is there a ruby equivalent?
Cross-platform solution:
First, install the Launchy gem:
$ gem install launchy
Then, you can run this:
require 'launchy'
Launchy.open("http://stackoverflow.com")
This should work on most platforms:
link = "Insert desired link location here"
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
system "start #{link}"
elsif RbConfig::CONFIG['host_os'] =~ /darwin/
system "open #{link}"
elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
system "xdg-open #{link}"
end
Mac-only solution:
system("open", "http://stackoverflow.com/")
or
`open http://stackoverflow.com/`
Simplest Win solution:
`start http://www.example.com`
Linux-only solution
system("xdg-open", "http://stackoverflow.com/")
This also works:
system("start #{link}")
Windows Only Solution:
require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute(...)
Shell Execute on MSDN
You can use the 'os' gem: https://github.com/rdp/os to let your operating system (in the best case you own your OS, meaning not OS X) decide what to do with an URL.
Typically this will be a good choice.
require 'os'
system(OS.open_file_command, 'https://stackoverflow.com')
# ~ like `xdg-open stackoverflow.com` on most modern unixoids,
# but should work on most other operating systems, too.
Note On windows, the argument(s?) to system need to be escaped, see comment section. There should be a function in Rubys stdlib for that, feel free to add it to the comments and I will update the answer.
If it's windows and it's IE, try this: http://rubyonwindows.blogspot.com/search/label/watir also check out Selenium ruby: http://selenium.rubyforge.org/getting-started.html
HTH

Resources