Skip to content

Instantly share code, notes, and snippets.

@roidrage
roidrage / ebooks.md
Created December 2, 2011 15:15
Self-published and awesome
#! /usr/bin/env ruby
# you can timeout in ruby even when all signals are blocked by doing the work
# in a child process (relaying the value back up a pipe) and doing the
# timeout-ing in the parent. of course this only works in POSIX systems
#
require 'timeout'
def timeoutx(seconds, &block)
#! /usr/bin/env ruby
status = DATA.flock(File::LOCK_EX | File::LOCK_NB)
if status == 0
puts "we have the lock..."
sleep
else
@igrigorik
igrigorik / webapp.rb
Created November 13, 2010 21:28
Inspired by @JEG2's talk at Rubyconf... Any ruby object, as a webapp! 'Cause we can. :-)
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
@tenderlove
tenderlove / itr.rb
Created October 22, 2010 23:06
itr.rb
class Metaclass
include Enumerable
def initialize obj
@obj = obj
end
def each
return enum_for(:each) unless block_given?
x = meta_for @obj