Module: Opener

Defined in:
lib/opener.rb

Overview

Ruby library for opening things in an cross-platform way.

Usage

Load this library:

require 'opener'

Open something in the foreground (blocking call):

Opener.system(thing_to_open_in_foreground)

Open something in the foreground while suppressing output:

Opener.system(thing_to_open_in_foreground, 1 => :close)

Open something in the foreground while suppressing errors:

Opener.system(thing_to_open_in_foreground, 2 => :close)

Open something in the background (non-blocking call):

Opener.spawn(thing_to_open_in_background)

Open something in the background while suppressing output:

Opener.spawn(thing_to_open_in_background, 1 => :close)

Open something in the background while suppressing errors:

Opener.spawn(thing_to_open_in_background, 2 => :close)

Reveal the OS-specific command that is opening things:

puts Opener.command()

License

This library is distributed under the same terms as Ruby: <www.ruby-lang.org/en/about/license.txt>

Copyright 2013 Suraj N. Kurapati <github.com/sunaku>

Thanks to 2010 David A. Wheeler <www.dwheeler.com/essays/open-files-urls.html>

Class Method Summary collapse

Class Method Details

.commandObject

Returns an OS-specific command for opening things.

www.dwheeler.com/essays/open-files-urls.html www.par.univie.ac.at/solaris/cde-www/



58
59
60
61
62
63
64
65
66
67
# File 'lib/opener.rb', line 58

def command
  @command ||=
    case RbConfig::CONFIG['host_os']
    when /darwin/i then 'open'
    when /cygwin/i then 'cygstart'
    when /linux|bsd/i then 'xdg-open'
    when /mswin|mingw/i then 'start'
    when /sunos|solaris/i then '/usr/dt/bin/sdtwebclient'
    end
end

.spawn(*arguments) ⇒ Object

Opens the given things in the background.

See Also:

  • Kernel#spawn()


84
85
86
87
# File 'lib/opener.rb', line 84

def spawn *arguments
  insert_command_into_arguments! arguments
  super
end

.system(*arguments) ⇒ Object

Opens the given things in the foreground.

See Also:

  • Kernel#system()


74
75
76
77
# File 'lib/opener.rb', line 74

def system *arguments
  insert_command_into_arguments! arguments
  super
end