Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

Rubyスクリプトからブラウザを呼び出すgem launchy

launchyはRubyスクリプトからブラウザを呼び出すgemです。
将来的にはメーラー等々も対応するようです。

sinatra等々と組み合わせるとlocalhost稼働なアプリを自動起動できます。
CLI上では

$ launchy http://ruby-lang.org/

Rubyスクリプト上では*1

uri = "http://www.ruby-lang.org"
Launchy.open( uri ) do |exception|
  puts "Attempted to open #{uri} and failed because #{exception}"
end

とします。

これだけだとバッククォートでのUNIXコマンド実行と大して変わらない気がしますが

$ launchy --help
Usage: launchy [options] thing-to-launch

Launch Options:
    -a, --application APPLICATION    Explicitly specify the application class to use in the launch
    -d, --debug                      Force debug. Output lots of information.
    -e, --engine RUBY_ENGINE         Force launchy to behave as if it was on a particular ruby engine.
    -n, --dry-run                    Don't launchy, print the command to be executed on stdout
    -o, --host-os HOST_OS            Force launchy to behave as if it was on a particular host os.

Standard Options:
    -h, --help                       Print this message.
    -v, --version                    Output the version of Launchy

と、実行環境を縛りたい場合はオプションをくっ付ける事で指定可能です。

*1:この例だと、例外が発生した場合キャッチするようになっています