Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open the default browser in Ruby

Tags:

browser

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?

like image 893
Gareth Simpson Avatar asked Sep 30 '08 11:09

Gareth Simpson


2 Answers

Cross-platform solution:

First, install the Launchy gem:

$ gem install launchy 

Then, you can run this:

require 'launchy'  Launchy.open("http://stackoverflow.com") 
like image 84
Ryan McGeary Avatar answered Sep 21 '22 12:09

Ryan McGeary


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 
like image 25
user1931928 Avatar answered Sep 22 '22 12:09

user1931928