Skip to content

Instantly share code, notes, and snippets.

View pda's full-sized avatar
💾
Formatting…

Paul Annesley pda

💾
Formatting…
View GitHub Profile
class A
def with(&block)
instance_eval(&block)
end
private
def private_blarg
puts 'this is from a private method'
end
end
#!/usr/bin/env ruby
# Contrived example of self-pipe preventing signal race condition prior to select()
# @see http://cr.yp.to/docs/selfpipe.html
# @author Paul Annesley
SELF_READ, SELF_WRITE = IO.pipe
@run = true
trap :INT do
#!/usr/bin/env ruby
#
# Are you one of the 10% of programmers who can write a binary search?
# http://reprog.wordpress.com/2010/04/19/are-you-one-of-the-10-percent/
def bin_search(search, subject)
discarded_left = 0
while subject.any?
#!/bin/bash
# Attempts to fast-forward the current local branch to its remote tracked branch.
# Safely refuses if there is no remote tracking, or if a fast-forward is not possible.
# @author Paul Annesley
if [ -n "$(git merge --ff-only 2>&1 | grep 'unknown option')" ]; then
echo "Your git doesn't seem to support --ff-only... try git 1.7+"
exit 1
fi
@pda
pda / bundlua.lua
Created July 2, 2011 12:33
bundlua: CSS/JavaScript bundler for lighttpd mod_magnet (from 2007)
-- bundlua: a css/javascript bundler written in LUA for lighttpd's mod_magnet
-- Paul Annesley : paul@annesley.cc : 2007-02-12
-- URL format: /bundle/file1.css,file2.css
-- include trailing slash for directories
local urlPrefix = '/bundle/'
local basedir = '/path/to/htdocs/'
local cssDir = basedir .. 'css/'
local jsDir = basedir .. 'js/'
local cacheDir = '/path/to/cache/dir/'
@pda
pda / stores_ip_address.rb
Created July 4, 2011 05:39
StoresIpAddress for ActiveRecord
module StoresIpAddress
extend ActiveSupport::Concern
included do
def self.stores_ip_address(attr)
define_method(attr) do
require "ipaddr"
@pda
pda / garbage_collection.rb
Created August 3, 2011 05:34
Faster RSpec with manual garbage collection
##
# spec/support/garbage_collection.rb
#
# Inspired by:
# http://www.rubyinside.com/careful-cutting-to-get-faster-rspec-runs-with-rails-5207.html
#
# At time of writing:
# reduces duration from ~16.7sec to ~15.7sec (6% improvement)
# increases peak memory usage from 150mb to 200mb (33% increase)
@pda
pda / composable_of_money.rb
Created August 18, 2011 05:03
ComposableOfMoney: currency attributes in ActiveRecord
##
# Aggregation
# http://ar.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html
module ComposableOfMoney
extend ActiveSupport::Concern
included do
@pda
pda / fat16.c
Created August 26, 2011 10:01
FAT16 filesystem reader, work in progress.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "fat.h"
#define FS_PATH "sample.fat16"
int main() {
print_boot_sector();
@pda
pda / unicorn.rake
Created September 12, 2011 07:39
Rake tasks for Unicorn: start stop restart increment decrement pstree
namespace :unicorn do
##
# Tasks
desc "Start unicorn"
task(:start) {
config = rails_root + "config/unicorn.rb"
sh "bundle exec unicorn --daemonize --config-file #{config}"
}