Skip to content

Instantly share code, notes, and snippets.

View mloughran's full-sized avatar

Martyn Loughran mloughran

View GitHub Profile
just a test
module IrregularScience
def resize_within(w, h)
r_old = width.to_f / height
r_new = w.to_f / h
w_new = r_new > r_old ? (h * r_old).to_i : w
h_new = r_new > r_old ? h : (w / r_old).to_i
self.resize(w_new, h_new) do |image|
yield image
#! /bin/sh
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'sudo update-rc.d nginx defaults', or use the appropriate command on your
# distro.
#
# Author: Ryan Norbauer <ryan.norbauer@gmail.com>
# Modified: Geoffrey Grosenbach http://topfunky.com
# Modified: Martyn Loughran http://mloughran.com
BEANSTALK_13 = '/opt/local/bin/beanstalkd'
BEANSTALK_14 = '/usr/local/bin/beanstalkd'
n = 100000
require 'rubygems'
require 'beanstalk-client'
require 'benchmark'
def teardown
require 'rubygems'
require 'eventmachine'
module EventMachine
# This returns a deferrable and catches errors in the block for you.
#
# Define callbacks and errbacks in usual way instead of weird EM.defer way
def self.better_defer?(&block)
deferrable = EM::DefaultDeferrable.new
class ExpectContinue
def initialize(app)
@app = app
end
def call(env)
if env['HTTP_EXPECT'] =~ /\A100-continue\z/i
[100, '', {}]
else
@app.call(env)
# Instances must supply a Klass.load method which is used to load the object
# in case it is not available in object space - for example from some data store.
# Also objects must provide an id method which is used as the key
#
module ObjectSpaceIdentityMap
def [](id)
obj = nil
ObjectSpace.each_object(self) do |o|
obj = o if o.id == id
end
class Command
class << self
def run(command)
output = `#{command} 2>&1`.chomp
if $? != 0
raise " *** Command `#{command}` failed with the following output:\n#{output}"
end
output
end
end
# Easily calculate mean and standard distribution of a distribution without
# collecting all values in memory
#
class Distribution
def initialize
@n, @sum_x, @sum_x_2 = 0, 0, 0
end
def <<(x)
@n += 1
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
class GoogleClosure
class Error < RuntimeError; end
HOST = URI.parse('http://closure-compiler.appspot.com/compile')