Skip to content

Instantly share code, notes, and snippets.

@lpar
lpar / gist:1320801
Created October 27, 2011 20:42
Generic skeleton template for a command line utility written in Ruby
#!/usr/bin/env ruby
# encoding: UTF-8
'di '
'ig00 '
# This is both a Ruby script and a man page; you can symlink it into your
# man directory as commandname.1 or run man -l on this file.
# This is a generic skeleton for a Ruby command-line utility, showing how to
# make the same file work as both a Ruby script and a man page, for ease of
# distribution. This cool hack brought to you by mathew <meta@pobox.com>.
@lpar
lpar / timeout.rb
Created June 17, 2011 20:41
Run a shell command in a separate thread, terminate it after a time limit, return its output
# Runs a specified shell command in a separate thread.
# If it exceeds the given timeout in seconds, kills it.
# Returns any output produced by the command (stdout or stderr) as a String.
# Uses Kernel.select to wait up to the tick length (in seconds) between
# checks on the command's status
#
# If you've got a cleaner way of doing this, I'd be interested to see it.
# If you think you can do it with Ruby's Timeout module, think again.
def run_with_timeout(command, timeout, tick)
output = ''