Skip to content

Instantly share code, notes, and snippets.

View betawaffle's full-sized avatar

Andrew Hodges betawaffle

View GitHub Profile
@defunkt
defunkt / connection_fix.rb
Created November 19, 2009 20:00
MySQL server has gone away fix
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
@betawaffle
betawaffle / trie.cpp
Created August 8, 2010 22:33
Trie WIP
#include "trie.h"
template <typename T> Trie<T>::Node&
Trie<T>::Node::operator [](char const* aString)
{
Node* node;
unsigned long s, e, i, n;
unsigned char r, l, c, d;
@betawaffle
betawaffle / process.pl
Created May 13, 2011 18:07
Linear Regex Replaces with Perl
#!/usr/bin/perl -w
use strict;
while (<>) {
s/\t/|/g; # Convert Tabs to Pipes
s/\r?\n/[]\r\n/g; # Add [] at EOL and Convert to CRLF
s/^\|+\[\]\r\n//g; # Remove Empty Lines
print;
@betawaffle
betawaffle / pong.coffee
Created June 24, 2011 00:35
Text-based Pong Game
util = require 'util'
after = (time, args..., callback) -> setTimeout callback, time, args...
class Game
score: 0
score_step: 20
constructor: (@client, @difficulty = 800) ->
@send 'HELO :Welcome to Pong!'
@send 'HELP :When you receive a PING <code>,'
@send 'HELP :reply quickly with PONG <code>,'
@betawaffle
betawaffle / application.html.erb
Created August 3, 2011 15:54
Controller Ancestry
<!DOCTYPE html>
<html>
<head>
<!-- Which allows you to do cool stuff like this: -->
<%= stylesheet_link_tag *controller.ancestry %>
<%= javascript_include_tag *controller.ancestry %>
<!-- So from within BarController, the stylesheets would look like this: -->
<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />
@betawaffle
betawaffle / missing.rb
Created August 25, 2011 12:49
Find the Missing Integer
def missing(list)
min, max, sum = list.reduce([f = list.first, f, 0]) do |(l, h, s), n|
[n < l ? n : l, n > h ? n : h, s + n]
end
max * (max + 1) / 2 - min * (min - 1) / 2 - sum
end
@betawaffle
betawaffle / pry-bench.rb
Created October 25, 2011 00:56
Pry Helpers
Pry.config.should_load_plugins = false
Pry.config.editor = proc { |file, line| "subl -w #{file}:#{line}" }
Pry.prompt = [
proc { |obj, nest_level| "#{RUBY_VERSION} (#{obj}):#{nest_level} > " },
proc { |obj, nest_level| "#{RUBY_VERSION} (#{obj}):#{nest_level} * " }
]
Pry.plugins['doc'].activate!
class BasicObject
@betawaffle
betawaffle / config.ru
Created October 25, 2011 16:59
Rack-based Local YARD Server
require 'rubygems'
require 'yard'
libs = {}
gems = {}
base = ENV['GEM_PATH'].split(':')[1][%r{^.+(?=/.+@global$)}] rescue nil
if base
Gem.paths = {
'GEM_PATH' => Dir["#{base}/ruby-*"].join(':'),
@brentertz
brentertz / rvm2rbenv.txt
Created November 21, 2011 23:00
Switch from RVM to RBENV
## Prepare ###################################################################
# Remove RVM
rvm implode
# Ensure your homebrew is working properly and up to date
brew doctor
brew update
## Install ###################################################################
-spec timestamp(atom(), binary(), object()) -> object().
%% Facebook Timestamp (eg. <<"2011-11-21T22:37:44+0000">>)
timestamp(Field, <<Year:4/binary, $-, Month:2/binary, $-, Day:2/binary, $T,
Hour:2/binary, $:, Minute:2/binary, $:, Second:2/binary, Offset:5/binary>>, Acc = #fb_object{}) ->
Date = {?BIN_TO_INT(Year), ?BIN_TO_INT(Month), ?BIN_TO_INT(Day)},
Time = {?BIN_TO_INT(Hour), ?BIN_TO_INT(Minute), ?BIN_TO_INT(Second)},
DateTime = case Offset of
<<"+0000">> -> % UTC
{Date, Time};
_ ->