Open the default browser in Ruby

RubyBrowser

Ruby Problem Overview


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?

Ruby Solutions


Solution 1 - Ruby

Cross-platform solution:

First, install the Launchy gem:

$ gem install launchy

Then, you can run this:

require 'launchy'

Launchy.open("http://stackoverflow.com")

Solution 2 - Ruby

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

Solution 3 - Ruby

Mac-only solution:

system("open", "http://stackoverflow.com/")

or

`open http://stackoverflow.com/`

Solution 4 - Ruby

Simplest Win solution:

start http://www.example.com

Solution 5 - Ruby

Linux-only solution

system("xdg-open", "http://stackoverflow.com/")

Solution 6 - Ruby

This also works:

system("start #{link}")

Solution 7 - Ruby

Windows Only Solution:

require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute(...)

Shell Execute on MSDN

Solution 8 - Ruby

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.

Solution 9 - Ruby

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGareth SimpsonView Question on Stackoverflow
Solution 1 - RubyRyan McGearyView Answer on Stackoverflow
Solution 2 - Rubyuser1931928View Answer on Stackoverflow
Solution 3 - RubyRyan McGearyView Answer on Stackoverflow
Solution 4 - RubyJames BakerView Answer on Stackoverflow
Solution 5 - Rubydamage3025View Answer on Stackoverflow
Solution 6 - RubypalmseyView Answer on Stackoverflow
Solution 7 - RubyKenView Answer on Stackoverflow
Solution 8 - RubyFelixView Answer on Stackoverflow
Solution 9 - RubyZsolt BotykaiView Answer on Stackoverflow