在Ruby中打开默认浏览器

时间:2008-09-30 11:51:37

标签: ruby browser

在Python中,你可以这样做:

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

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

是否有红宝石等同物?

9 个答案:

答案 0 :(得分:85)

跨平台解决方案:

首先,安装Launchy gem:

$ gem install launchy

然后,你可以运行:

require 'launchy'

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

答案 1 :(得分:32)

这适用于大多数平台:

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

答案 2 :(得分:31)

Mac-only解决方案:

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

`open http://stackoverflow.com/`

答案 3 :(得分:9)

最简单的赢解决方案:

`start http://www.example.com`

答案 4 :(得分:8)

仅限Linux的解决方案

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

答案 5 :(得分:4)

这也有效:

system("start #{link}")

答案 6 :(得分:3)

仅限Windows解决方案:

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

Shell Execute on MSDN

答案 7 :(得分:1)

您可以使用'os'gem:https://github.com/rdp/os来让您的操作系统(最好是您拥有自己的操作系统,而不是OS X)决定如何处理URL。

通常这是一个不错的选择。

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.

答案 8 :(得分:0)

如果它是windows并且它是IE,请试试这个:http://rubyonwindows.blogspot.com/search/label/watir还要查看Selenium ruby​​:http://selenium.rubyforge.org/getting-started.html

HTH

相关问题