Skip to content

Instantly share code, notes, and snippets.

class Array
# Converts [[k1,v1],[k2,v2]] into {k1 => v1, k2 => v2}
# The opposite of Hash#to_a
# Same as `Hash[ary.flatten]`
def to_h
inject({}) {|h,el| h[el[0]] = el[1]; h }
end
end
#/bin/bash
# Builds a gc patched ruby according to:
# http://guides.rubyonrails.org/benchmarking_and_profiling.html#_performance_test_cases
mkdir ${HOME}/rubygc
mkdir -p ${HOME}/src
# 1. Compile
echo "-- compiling ruby from source with gc patch --"
cd ${HOME}/src
@bmarini
bmarini / backup-sites
Created January 4, 2009 20:51
A quick and dirty website backup script
#!/bin/bash
# a quick and dirty backup script.
# if you have a websites folder and some databases to backup
# this script might be a good starting point.
# this script assumes you have user named admin.
BASE_DIR='/'
TARGET_DIR='sites'
BACKUP_TIME=`date +%Y%m%d%H%I%S`
BACKUP_FILENAME="${BACKUP_TIME}-${TARGET_DIR}"
=== Epic Snow Leopard Upgrayyyyd Guide ===
More info here: http://www.gregbenedict.com/2009/08/29/fixing-ruby-gems-mysql-and-passenger-phusion-on-snow-leopard-10-6/
Son, you’re now living in the land of 64-bit systems.
That means that some of your 32-bit shit is now broken.
Not all is lost.
Example for .bashrc:
export LSCOLORS="exfxcxdxbxegedabagacad"
a black
b red
c green
d brown
e blue
f magenta
g cyan
sudo atsutil databases -remove
atsutil server -shutdown
atsutil server -ping
@bmarini
bmarini / average_time_of_day.rb
Created October 19, 2009 01:07
Solution to RPCFN #2
module AverageTimeOfDay
require 'date'
# Assumes times_of_day is an array of valid time strings in chronological
# order.
def average_time_of_day(times_of_day)
# Convert times_of_day to an array of Time objects, ensuring that times
# are in chronological order by adding a day if the the time of the
# current iteration is earlier than the last iterated time.
times = times_of_day.inject([]) do |times, time_of_day|
dt = DateTime.strptime(time_of_day, "%I:%M%p")
# This is a skeleton for a daemon process that behaves well in
# a unix environment.
# Fork a child process and exit the parent
exit(0) if Process.fork
# Trap the signal sent by the kill command
trap("SIGTERM") { Syslog.warning("I died"); exit(0) }
# Establish this process as a new session and process group leader,
module Rack
# Renders a valid robots.txt according to http://www.robotstxt.org
# This is an example of over-engineering. But it's simple example of
# how you might test your middleware.
class RobotsTxt
def initialize(app)
@app = app
end
def call(env)
@bmarini
bmarini / closed-circuit.rb
Created November 1, 2009 23:16
Solution to RPCFN #3
require 'set'
class ClosedCircuit
Infinity = 1/0.0
def initialize(input, source, dest)
@graph = GraphParser.parse(input)
@source = source
@dest = dest
end