61

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?

Gareth Simpson
  • 36,943
  • 12
  • 47
  • 50

9 Answers9

92

Cross-platform solution:

First, install the Launchy gem:

$ gem install launchy

Then, you can run this:

require 'launchy'

Launchy.open("http://stackoverflow.com")
Ryan McGeary
  • 235,892
  • 13
  • 95
  • 104
  • This doesn't appear to work with data urls. (Have you got it to work with data urls? If not, have another suggestion?) – JellicleCat Aug 26 '11 at 17:40
  • 2
    In theory, it should work with any scheme registered with the operating system. Data URIs typically aren't registered with the OS. – Ryan McGeary Jan 12 '12 at 21:21
  • Can you tell me if i can check or not if the opened url using launchy is closed or terminated or not? @RyanMcGeary – Harshit Laddha Apr 28 '14 at 15:40
  • @deadman nope - although you could use javascript or a plugin to call a sever which tells the program (probably overkill) – Ben Aubin Jan 24 '16 at 21:09
  • 2016/6/17 - link broken - dunno if it is just temporarily down, gone now, or moved. https://github.com/copiousfreetime/launchy works though. – PressingOnAlways Jun 17 '16 at 20:58
35

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
user1931928
  • 351
  • 3
  • 4
  • This is great, thanks! Just don't forget to forget to quote those strings in case there are metacharacters which would confuse the shell—an & in a URL query string is a classic example. Wrap the URL in single quotes for Mac, Linux, and BSD; double quotes for Windows. On Windows: an extra pair of double quotes are required (_i.e._, `system "start \"\" \"#{link}\""`) because of a quirk in the way `start` handles quoted arguments. – TheDudeAbides Jul 31 '20 at 14:23
32

Mac-only solution:

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

or

`open http://stackoverflow.com/`
Ryan McGeary
  • 235,892
  • 13
  • 95
  • 104
9

Simplest Win solution:

`start http://www.example.com`
James Baker
  • 5,978
  • 3
  • 25
  • 28
8

Linux-only solution

system("xdg-open", "http://stackoverflow.com/")
damage3025
  • 81
  • 1
  • 1
4

This also works:

system("start #{link}")
palmsey
  • 5,812
  • 3
  • 37
  • 41
3

Windows Only Solution:

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

Shell Execute on MSDN

Ken
  • 2,092
  • 1
  • 19
  • 16
3

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.

Felix
  • 4,510
  • 2
  • 31
  • 46
  • The only solution that worked for me with various params in the url – Yoni Jan 03 '21 at 15:40
  • OS.open_file_command internally does the same as @user1931928's answer. And it has the same problem with quotes pointed out in the comments – Basil Peace Feb 04 '22 at 22:13
  • @BasilPeace good to know. I guess the `os` gem is better maintained than user1931928's answer though ;) Not having research time now, but I guess `Shellwords` would be the answer wrt to escaping, right? I'll gladly add a hint to my answer. – Felix Feb 05 '22 at 19:05
  • @BasilPeace Are you sure, though? As I pass as **second argument** to the `system` call. Thats pretty different from the case where string interpolation is used. – Felix Feb 06 '22 at 07:45
  • @Felix, I ran into the issue under Windows, with URLs containing an ampersand (`&`). Looks like `os` gem didn't escape them and just passed them to `cmd` (or `start`?). And the latter considered `&` as a command separator – Basil Peace Feb 07 '22 at 00:13
  • I chose Launchy, no issues so far – Basil Peace Feb 07 '22 at 00:13
  • @Felix, uh, are you saying that I incorrectly blame `os` gem? The problem is not in there, but in your `system` call? – Basil Peace Feb 07 '22 at 00:20
  • @BasilPeace Peace. I did not blame anybody. Please share with me where the `os` gem did not escape the URL. In the answer I give here, the URL is handled by "system", `os` is just used to return a string (the name of the command). I do not own a windows license, but you are also suggesting that `system("start", "https://stackoverflow.com&evilcommand")` (important: two arguments!) will execute `evilcommand` in Windows? Then it is indeed something to look for into `Kernel.system`. – Felix Feb 07 '22 at 05:54
  • 1
    @Felix, yes. https://imgur.com/a/oaAhxL9 – Basil Peace Feb 08 '22 at 07:32
  • 1
    @BasilPeace okay, thanks. I will update the description. Smells a bit like a but to me, but cannot judge. – Felix Feb 08 '22 at 11:06
0

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

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110