在Ruby中打开默认浏览器

在Python中,您可以执行以下操作:

import webbrowser
webbrowser.open_new("http://example.com/")

它将在默认浏览器中打开传入的网址

有红宝石吗?

Gareth Simpson asked 2019-11-06T21:17:30Z
8个解决方案
87 votes

跨平台解决方案:

首先,安装Launchy gem:

$ gem install launchy

然后,您可以运行以下命令:

require 'launchy'

Launchy.open("http://stackoverflow.com")
Ryan McGeary answered 2019-11-06T21:17:59Z
32 votes

仅限Mac的解决方案:

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

要么

`open http://stackoverflow.com/`
Ryan McGeary answered 2019-11-06T21:18:23Z
32 votes

这应该适用于大多数平台:

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 answered 2019-11-06T21:18:47Z
9 votes

最简单的Win解决方案:

`start http://www.example.com`
James Baker answered 2019-11-06T21:19:10Z
8 votes

仅限Linux的解决方案

system("xdg-open", "http://stackoverflow.com/")
damage3025 answered 2019-11-06T21:19:33Z
4 votes

这也有效:

system("start #{link}")
palmsey answered 2019-11-06T21:19:51Z
3 votes

仅Windows解决方案:

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

在MSDN上执行Shell

Ken answered 2019-11-06T21:20:21Z
0 votes

如果是Windows且是IE,请尝试以下操作:[http://rubyonwindows.blogspot.com/search/label/watir]还请检查硒红宝石:[http://selenium.rubyforge.org/getting-started.html]

高温超导

Zsolt Botykai answered 2019-11-06T21:20:51Z
translate from https://stackoverflow.com:/questions/152699/open-the-default-browser-in-ruby