Skip to content

Instantly share code, notes, and snippets.

View aalin's full-sized avatar

Andreas Alin aalin

  • Peru
View GitHub Profile
@aalin
aalin / figure_out_chord.rb
Created April 14, 2010 20:23
Figure out chord intervals
# Program output:
#
# Chord: [0, 4, 7]
# [:major, [[0, :unison], [4, :third], [7, :fifth]]]
# [:minor, [[0, :unison], [3, :third], [8, :sixth]]]
# [:major, [[0, :unison], [5, :fourth], [9, :sixth]]]
# Chord: [0, 4, 7, 11]
# [:major, [[0, :unison], [4, :third], [7, :fifth], [11, :seventh]]]
# [:minor, [[0, :unison], [3, :third], [7, :fifth], [8, :sixth]]]
# [:major, [[0, :unison], [4, :third], [5, :fourth], [9, :sixth]]]
@aalin
aalin / .irbrc
Created October 8, 2010 10:05
Show rails environment in the rails console
class IRB::Irb
alias :original_prompt :prompt
def prompt(prompt, ltype, indent, line_no)
prompt = prompt.call if prompt.respond_to?(:call)
original_prompt(prompt, ltype, indent, line_no)
end
end
IRB.conf[:PROMPT_MODE] = :RAILS_ENV
IRB.conf[:PROMPT][:RAILS_ENV] = IRB.conf[:PROMPT][:CLASSIC].merge(:PROMPT_I => lambda { (defined?(Rails) ? "#{Rails.env} " : "") + "%N(%m):%03n:%i> " })
@aalin
aalin / bellevue.rb
Created October 8, 2010 13:11
Prints the menu from restaurangbellevue.se
#!/usr/bin/env ruby
require 'iconv'
require 'open-uri'
class String
COLORS = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
def colorize(color)
value = case color
when Symbol
@aalin
aalin / runonupdate
Created October 18, 2010 09:50
Run a command when a file has been changed
#!/usr/bin/env ruby
# Usage:
# runonupdate "spec spec/models/cat_spec.rb -O spec/spec.opts" app/models/cat.rb app/models/dog.rb
#
# This will run cat-specs whenever cat.rb or dog.rb is updated.
class RunOnUpdate
attr_reader :cmd
attr_reader :files
@aalin
aalin / multibench.rb
Created October 26, 2010 09:52
multibench: useful for benchmarking columns in html tables for example.
class Multibench
def initialize
@benches = Hash.new { 0 }
end
def bench(title)
start = Time.now
yield.tap do
@benches[title] += Time.now - start
end
@aalin
aalin / pivotalstatus.rb
Created June 22, 2011 13:06
Pivotal tracker git status thing...
#!/usr/bin/env ruby
require 'rubygems'
require 'pivotal-tracker'
class String
ANSI_COLORS = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
def colorize(color)
num = ANSI_COLORS.index(color) or raise("Bad color #{ color }")
"\e[3#{ num }m" + self + "\e[0m"
@aalin
aalin / commit-msg
Created July 15, 2011 08:28
git commit-msg hook that replaces STORYID with the story id from the branch name (s-123, s123, t123, t-123)
#!/usr/bin/env ruby
if match = `git symbolic-ref HEAD 2> /dev/null`.split('/').last.to_s.match(/^(?:t|s)-?(\d+)$/)
message = File.read(ARGV.first)
message.gsub!("STORYID", "[##{ match[1] }]")
File.open(ARGV.first, 'w') do |f|
f.puts(message)
end
end
@aalin
aalin / gist:1210945
Created September 12, 2011 09:54
Diff times so that you can read them in rspec
require 'rspec-expectations'
class RSpec::Expectations::Differ
def diff_as_object(a, b)
if a.is_a?(Time) && b.is_a?(Time)
minutes, seconds = (b - a).divmod(60)
hours, minutes = minutes.divmod(60)
Kernel.format(" %dh %dm %ds \nactual: %s\nexpected: %s", hours, minutes, seconds % 60, b.utc.to_s, a.utc.to_s)
else
super
ZSH_THEME_GIT_PROMPT_PREFIX=" %F{208}❨%F{220}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%F{208}❩%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%F{196}✻%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN=""
PROMPT=$'%F{196}%M %F{202}%~%{$reset_color%}$(git_prompt_info)%{\e[%(?.32.31);1m%} ➔%{$reset_color%} '
@aalin
aalin / vim.rb
Created October 11, 2011 07:19 — forked from uasi/vim.rb
Vim formula for Homebrew
require 'formula'
class Vim < Formula
homepage 'http://www.vim.org/'
url 'ftp://ftp.vim.org/pub/vim/unix/vim-7.3.tar.bz2'
head 'https://vim.googlecode.com/hg/'
sha256 '5c5d5d6e07f1bbc49b6fe3906ff8a7e39b049928b68195b38e3e3d347100221d'
version '7.3.294'
def features; %w(tiny small normal big huge) end