Tests build confidence. Write 'em. They'll save your ass, and they'll let you take a chainsaw to your code without being afraid of unintended consequences.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Parallel map | |
# Originally from http://project.ioni.st/post/2332#snippet_2332 | |
class Array | |
def pmap | |
threads = [] | |
0.upto(size - 1) do |n| | |
threads << Thread.new do | |
yield(at(n)) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module URI | |
def self.parse_with_options(uri_str, opts = {}) | |
parsed_uri = self.parse(uri_str) | |
sing = (class << parsed_uri; self end) | |
sing.send :define_method, :options do | |
opts | |
end | |
parsed_uri | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# capistrano tip: | |
# log into target server for first time an type this: | |
ssh git@github.com | |
# this will allows capistrano to perform the clone without issue. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'xmpp4r-simple' | |
require 'pp' | |
im = Jabber::Simple.new("user", "password") | |
Jabber::debug = true | |
class Handler | |
def on_message(message) | |
puts message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$:.unshift File.dirname(__FILE__) + "/lib" | |
require "oauth/consumer" | |
require "pp" | |
consumer = OAuth::Consumer.new( | |
"<consumer.key>", | |
"<consumer.secret>", | |
{ | |
:site => "http://api.netflix.com", | |
:request_token_url => "https://api-user.netflix.com/oauth/request_token", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rails_root = "/data/github/current" | |
20.times do |num| | |
God.watch do |w| | |
w.name = "dj-#{num}" | |
w.group = 'dj' | |
w.interval = 30.seconds | |
w.start = "rake -f #{rails_root}/Rakefile production jobs:work" | |
w.uid = 'git' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) Point *.example.com in your DNS setup to your server. | |
# | |
# 2) Setup an Apache vhost to catch the star pointer: | |
# | |
# <VirtualHost *:80> | |
# ServerName *.example.com | |
# </VirtualHost> | |
# | |
# 3) Set the current account from the subdomain | |
class ApplicationController < ActionController::Base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# at the very least, use inheritance! | |
class Object | |
alias_method :try, :send | |
end | |
class NilClass | |
def try(*args) | |
nil | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc "Do rolling restarts of the Mongrel servers in pairs, at 90s intervals." | |
task :rolling do | |
servers = [] | |
roles[:app].each do |server| | |
next unless server.options.fetch(:listener, true) | |
servers << [] if servers.first.nil? || servers.last.length > 1 | |
servers.last << server.host | |
end | |
OlderNewer