Skip to content

Instantly share code, notes, and snippets.

class HashWithIndifferentAccess < Hash
def [](key)
super(convert_key(key))
end
def []=(key, value)
super(convert_key(key), value)
end
def self.[] (*args)
@gavinheavyside
gavinheavyside / .gitconfig
Created June 20, 2014 09:12
Clean up your leftover merged git branches
[alias]
cleanup = "!git fetch -p && git branch --merged | grep -v '\\*\\|master\\|develop' | xargs -n 1 git branch -d"
// example program
package main
import (
"fmt"
"os"
"github.com/garyburd/redigo/redis"
)
class Crap
attr_accessor :crappiness
def initialize(tolerability = 5)
@crappiness = 0
@tolerability = tolerability
end
def +(int)
result = dup
@gavinheavyside
gavinheavyside / hash_differences.rb
Last active January 4, 2016 16:39
Hash[] difference between Ruby 1.9 and 2.x
# Ruby 1.9:
Hash[ [ "", "key:value" ].map { |v| v.split( /:/) } ]
# => {"key"=>"value"}
# Ruby 2.0+:
Hash[ [ "", "key:value" ].map { |v| v.split( /:/) } ]
# ArgumentError: invalid number of elements (0 for 1..2)
# from (pry):1:in `[]'
@gavinheavyside
gavinheavyside / fix-credit-card-qif.rb
Created September 25, 2013 17:43
Reverse signs of transactions in a qif file, and open it (OS X) in the associated program. My bank generates .qif files for my credit card with credit & debit reversed compared with how my finance program wants them.
#!/usr/bin/env ruby
require 'qif'
if ARGV.size != 1
$stderr.puts "usage: fix-credit-card-qif-and-load.rb <CC qif file>"
end
input_filename = ARGV[0]
output_filename = input_filename.sub(/\.qif/, "-fixed.qif")
@gavinheavyside
gavinheavyside / fizzbuzz.rb
Last active September 27, 2019 08:43 — forked from mrb/fizzbuzz.rb
Writing FizzBuzz without modulus division or 'if'
fizz = [nil, nil, "Fizz"].cycle
buzz = [nil, nil, nil, nil, "Buzz"].cycle
(1..100).zip(fizz, buzz) do |num, *fb|
puts fb.compact.reduce(:+) || num
end
@gavinheavyside
gavinheavyside / gist:3761159
Created September 21, 2012 12:14
Problem report from Tweetbot beta crash on 10.7.5
Process: Tweetbot [10718]
Path: /Applications/Tweetbot.app/Contents/MacOS/Tweetbot
Identifier: com.tapbots.TweetbotMacAdHoc
Version: 0.8.0 (758)
Code Type: X86-64 (Native)
Parent Process: launchd [261]
Date/Time: 2012-09-21 13:10:53.983 +0100
OS Version: Mac OS X 10.7.5 (11G56)
Report Version: 9
@gavinheavyside
gavinheavyside / array_aggregator.rb
Created July 19, 2012 19:20
a solution to the alphasights job quiz
# As seen in Ruby Weekly newsletter http://rubyweekly.com/archive/102.html
# http://www.alphasights.com/apply/ruby-developer-london
by_time = log.reduce(Hash.new{|hash,key| hash[key] = {}}) do |memo, val|
memo[val[:time]].merge!(val.reject{|k,v| k == :time})
memo
end
by_time.map{|k,v| v.merge(time: k)}
@gavinheavyside
gavinheavyside / Invoking
Created May 6, 2012 11:55
strip colons from filenames in an extracted report tarball
# from the top level of an extracted report tarball
$ find . -type d -depth 1 | xargs -I {} ./filename_fixer.sh {}