Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Open URI with default web browser
# -*- coding: utf-8 -*-
require 'uri'
class WebBrowser
def self.open(uri)
uri = URI.parse(uri.to_s)
unless %w[ http https ftp file ].include?(uri.scheme)
raise ArgumentError
end
case RUBY_PLATFORM
when /mswin(?!ce)|mingw|bccwin/
system %!start /B #{uri}!
when /cygwin/
system %!cmd /C start /B #{uri}!
when /darwin/
system %!open '#{uri}'!
when /linux/
system %!xdg-open '#{uri}'!
when /java/
require 'java'
import 'java.awt.Desktop'
import 'java.net.URI'
Desktop.getDesktop.browse java.net.URI.new(uri)
else
raise NotImplementedError
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.