Skip to content

Instantly share code, notes, and snippets.

View betawaffle's full-sized avatar

Andrew Hodges betawaffle

View GitHub Profile
@betawaffle
betawaffle / gist:3092664
Created July 11, 2012 19:33
Airbrake Deploy
function ab-deploy {
api_key=$AIRBRAKE_API_KEY
env=${1:-development}
sha=$(git rev-parse ${1:-HEAD} 2>/dev/null)
user=$USER
repo=$REPO
body="api_key=$api_key&deploy[rails_env]=$env&deploy[local_username]=$user&deploy[scm_repository]=$repo&deploy[scm_revision]=$sha"
echo -n "curl -d \"$body\" https://api.airbrake.io/deploys.txt" | pbcopy
echo "Copied curl command to clipboard!"
}
-type folder(T, Acc) :: fun((T, Acc) -> Acc).
-type byte_folder(Acc) :: folder(byte(), Acc).
-spec foldl_bytes(byte_folder(Acc), Acc, io()) -> Acc.
foldl_bytes(Fun, Acc, Binary) when is_binary(Binary) ->
foldl_binary_bytes(Fun, Acc, Binary);
foldl_bytes(Fun, Acc, IoList) when is_list(IoList) ->
foldl_iolist_bytes(Fun, Acc, IoList).
-spec foldl_binary_bytes(byte_folder(Acc), Acc, binary()) -> Acc.
sign(Req, Auth) ->
Params = Req#req.params,
Method = Req#req.method,
Url = Req#req.url,
{H, P} = oauth_params(Params, Auth),
Sig = signature(Method, Url, P, Auth),
IoList = ["OAuth ",
"oauth_signature", $=, Sig
|H],
Req#req{headers = [{"Authorization", iolist_to_string(IoList)}
-spec permission_name(binary()) -> atom() | binary().
permission_name(<<"create_note">>) -> create_note;
permission_name(<<"installed">>) -> installed;
permission_name(<<"manage_pages">>) -> manage_pages;
permission_name(<<"offline_access">>) -> offline_access;
permission_name(<<"photo_upload">>) -> photo_upload;
permission_name(<<"publish_stream">>) -> publish_stream;
permission_name(<<"read_insights">>) -> read_insights;
permission_name(<<"read_stream">>) -> read_stream;
permission_name(<<"share_item">>) -> share_item;
% Fuck you, erlang!
mem_get(Addr, <<_:Addr/unit:?WORD, Word:1/binary-unit:?WORD, _/binary>>) ->
Word.
module ::RubyVM::Helpers
class << self
def compile_option name
line = __LINE__ + 2
code = <<-RUBY
def #{name}
::RubyVM::InstructionSequence.compile_option[:#{name}]
end
def #{name}= bool
def pi k = nil
if k
a = Rational(4, 8 * k + 1)
b = Rational(2, 8 * k + 4)
c = Rational(1, 8 * k + 5)
d = Rational(1, 8 * k + 6)
return (a - b - c - d) * (16 ** -k)
end
UnboundMethod.class_exec do
def to_proc
method = self
lambda { |*args, &block| method.bind(self).call(*args, &block) }
end
end
@betawaffle
betawaffle / y.rb
Created March 25, 2012 21:00
Ruby Y Combinator
def Y f = nil, &b
f ||= b; -> g { g[g] }.call -> g { f[-> *a { g[g][*a] }] }
end
# Hash Autovivification
Y { |d| -> { Hash.new do |h, k| h[k] = d[] end }[]
# Factorial
Y -> fac { -> n { n <= 2 ? n : n * fac[n - 1] } }
class FormattedString < String
PATTERN = /:(\w+)/
DEFER = lambda { |match| match }
def initialize str = '', pattern = nil
super str
pattern ||= PATTERN
@compiler = DEFER