Skip to content

Instantly share code, notes, and snippets.

View avdgaag's full-sized avatar

Arjan van der Gaag avdgaag

View GitHub Profile
@avdgaag
avdgaag / post-checkout.sh
Created March 19, 2013 19:30
Git post-checkout hook that automatically updates your bundled gems (if applicable), re-generates your tags index using exuberant ctags, and tries to advise you about pending migrations.
#!/bin/sh
set -e
OLDREV=$1
NEWREV=$2
IS_BRANCH_CHECKOUT=$3
function has_changes_to() {
git diff --name-only "$OLDREV" "$NEWREV" -- "$1" | grep "$1" > /dev/null
@avdgaag
avdgaag / auto_reference_pt_ids.rb
Last active December 15, 2015 02:39
Auto-labeling Git commits with Pivotal Tracker IDs found in the current branch name. This script is meant to be used as a Git `commit-msg` hook. It expects a single argument, the location of the temporary file with the commit message the user entered, and it outputs that same message with maybe some extra labels added to it.
#!/usr/bin/env ruby
# ## Introduction
#
# Auto-labeling Git commits with issues tracker IDs found in the current
# branch name. This script is meant to be used as a Git commit-msg hook. It
# expects a single argument, the location of the temporary file with the commit
# message the user entered, and it outputs that same message with maybe some
# extra labels added to it.
#
# ## Example
@avdgaag
avdgaag / DojoS2E6.sh
Last active December 10, 2015 09:39
Shell one-liner to create a histogram from a log file.
export COL=$COLUMNS; awk -F '[ ()]|ms' '/\[CatalogItem\] Retrieved/ { c[int($8 / $6 / 100) * 100 + 50] += $6 } END { for(l in c) { print l, c[l] } }' production.log | awk 'BEGIN { max = 0 } { if($2 > max) max = $2; c[$1] = $2 } END { for(l in c) { printf("%5d %d\n", l, c[l] / max * (ENVIRON["COL"] - 7)) } }' | awk '{ r = ""; s = $2; while(s-- > 0) { r = r "#" }; printf("%6d %s\n", $1, r); }' | sort -n
@avdgaag
avdgaag / command.sh
Last active December 9, 2015 23:49 — forked from anonymous/gist:4346624
Shell one-liner using Ruby to create a histogram from a log file.
export COL=$COLUMNS ; grep '\[CatalogItem\] Retrieved' production.log | awk '{ print $6 $7 }' | ruby -ne 'BEGIN { puts ENV.inspect;$columns = ENV["COL"].to_i; $all = Hash.new { |h,k| h[k] = 0 } }; a, b = $_.split("(").map(&:to_f); next if a < 1; $all[((b/a/100).floor)] += a.to_i; END { max_value = $all.values.max; puts "\e[2J\e[f"; puts "Average request time".center($columns); print "\033[37m" ; puts "ms/req ±50ms".center($columns); print "\033[0m" ; puts ("─" * $columns); $all.keys.sort.each { |k| puts "#{(k * 100 + 50).to_s.rjust(6)}: \033[31m#{"▓" * ($all[k].to_f / max_value * ($columns - 8))}\033[0m"; }; puts "─" * $columns; print "\033[5m\033[35m" ; puts "(╯°□°)╯︵ ┻━┻ © Arjan & Ariejan".center($columns); print "\033[0m" }'
@avdgaag
avdgaag / gem_template.rb
Created September 29, 2012 10:53
Ruby script to generate a custom Ruby Gem structure, including all sorts of files for RSpec, Yard, Travis etc.
require 'fileutils'
NAME = ARGV[0]
MODULE_NAME = NAME.gsub(/(?:^|_)(.)/) { $1.upcase }
ROOT = File.join(FileUtils.pwd, NAME)
def in_dir(dir)
old_dir = FileUtils.pwd
FileUtils.cd dir
yield
FileUtils.cd old_dir
@avdgaag
avdgaag / gist:3517092
Created August 29, 2012 18:54
Solution to minefield code kata
class SurroundingRange
attr_reader :point, :field
def initialize(point, field)
@point, @field = point, field
end
def points
horizontal = [point.x - 1, 0].max..[point.x + 1, field.width - 1].min
vertical = [point.y - 1, 0].max..[point.y + 1, field.height - 1].min
@avdgaag
avdgaag / sinatra_template.rb
Created July 29, 2012 14:07
Quickly generate a basic Sinatra application
require 'fileutils'
NAME = ARGV[0]
MODULE_NAME = NAME.gsub(/(?:^|_)(.)/) { $1.upcase }
ROOT = File.join(FileUtils.pwd, NAME)
def in_dir(dir)
old_dir = FileUtils.pwd
FileUtils.cd dir
yield
FileUtils.cd old_dir
@avdgaag
avdgaag / _media-queries.scss
Created May 29, 2012 10:54 — forked from anthonyshort/_media-queries.scss
Media Queries in Sass
// Media Queries in Sass 3.2
//
// These mixins make media queries a breeze with Sass.
// The media queries from mobile up until desktop all
// trigger at different points along the way
//
// And important point to remember is that and width
// over the portrait width is considered to be part of the
// landscape width. This allows us to capture widths of devices
// that might not fit the dimensions exactly. This means the break
@avdgaag
avdgaag / roman_numerals.py
Created April 24, 2012 18:29
Kata: Roman Numerals
import unittest
def toRoman(n):
"""
Convert an integer to Roman numerals. Returns an empty string for 0.
"""
if n == 0:
return ''
map = { 1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL', 50: 'L', 90: 'XC', 100: 'C' }
key = max(k for k, v in map.iteritems() if k <= n)
@avdgaag
avdgaag / game.rb
Created March 2, 2012 08:58
Text based fighting game
# Make sure you have eventmachine installed and run the server:
#
# ruby -rubygems game.rb
#
# When the server is running, clients can connect and issue commands.
# Currently supported commands:
#
# * look: list the players in the arena.
# * join [nickname]: join the arena using a nickname
# * leave: leave the arena