Skip to content

Instantly share code, notes, and snippets.

@localshred
localshred / sentence_parsing.rb
Created December 21, 2011 23:35
Evented Sentence Parser
require 'eventually'
class SentenceParser
include Eventually
def initialize(document)
@document = document
end
def parse!
@localshred
localshred / cherry_compare.sh
Created January 4, 2012 18:47
Check for out-of-date branches in a git-flow style branch system
function cherry_compare() {
echo 'Out-of-date branch check'
echo '[master -> qa]'
git cherry -v qa master
echo '[qa -> stage]'
git cherry -v stage qa
echo '[stage -> stable]'
@localshred
localshred / git_helper_functions.sh
Created February 16, 2012 21:02
git helpers for mainline branch synchronization and checking
# Synchronize all mainline branches (master, qa, stage, stable)
function reposync() {
git fetch
git checkout master
git rebase origin/master
git checkout qa
git rebase origin/qa
git checkout stage
git rebase origin/stage
git checkout stable
@localshred
localshred / puts_watcher.rb
Created February 28, 2012 00:02
Have stdout/err output you just can't track down? Use this. It needs to be inserted in the earliest possible load path (like spec_helper or rails boot.rb)
def STDOUT.puts *args
super(*args)
STDERR.puts(caller[0..5]) if args.any?{|a| a.to_s =~ /my semi-unique string/ }
end
@localshred
localshred / binary_search.rb
Created February 28, 2012 07:22
Land of Lisp binary search algorithm in ruby. This is in stark contrast to my verbose approach in the summer of 2011: http://rand9.com/blog/picking-the-right-number-as-quickly-as-possible/
# set some vars
@high = 100
@low = 0
# Simply calc the midpoint (no remainder)
# of the high and low value (bit shift right)
def ask
(@high + @low) >> 1
end
@localshred
localshred / 1.9.2_ex.irb
Created March 6, 2012 23:57
1.9.3 changes the behavior of "rescue else" in a method from previous ruby versions. A return in the method body will not trigger the else.
>> hmm
hello
I got here
in ensure
@localshred
localshred / bdays.rb
Created May 18, 2012 17:27
Birthdays date calculator to show num days alive, leap years lived through, average age of a group of people (e.g. your fam). I dunno, fun to write at least.
require 'date'
today = Date.today
peeps = [
[:bj, 1983, 1, 18],
[:angelee, 1983, 12, 27],
[:bella, 2004, 11, 17],
[:anders, 2008, 6, 30],
[:india, 2012, 2, 23]
@localshred
localshred / symbolized_hash_keys.rb
Created August 9, 2012 17:56
Symbolize keys doesn't do what I expected when to_sym equivalent keys exist.
# Fascinating behavior of symbolize_keys usage. If you have two keys that
# are `to_sym` equivalents, the non-symbol version is the winner,
# regardless of the order in the hash.
# For some reason I expected if a symbol key existed it would be
# the winner.
1.9.2p290 :002 > {"one" => 1, :one => 2}.symbolize_keys
=> {:one=>1}
1.9.2p290 :001 > {:one => 1, "one" => 2}.symbolize_keys
=> {:one=>2}
@localshred
localshred / gist:3362147
Created August 15, 2012 18:24
View shake
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setDuration:0.08];
[animation setRepeatCount:3];
[animation setAutoreverses:YES];
[animation setFromValue:[NSValue valueWithCGPoint:
CGPointMake([formContainer center].x - 20.0f, [formContainer center].y)]];
animation setToValue:[NSValue valueWithCGPoint:
CGPointMake([formContainer center].x + 20.0f, [formContainer center].y)]];
[[formContainer layer] addAnimation:animation forKey:@"position"];
@localshred
localshred / pre-receive.rb
Created August 29, 2012 02:29
Reject commits if updating non-dev branch and the Gemfile was changed and contains a list of rejectable words (e.g. git, tag, branch, path, etc.)
#!/usr/bin/env ruby
BRANCHES = %w( qa stage stable)
REJECT_OPTIONS = %w( git tag branch path )
old_sha, new_sha, ref = STDIN.read.split(' ')
exit 0 unless BRANCHES.include?(ref.split('/').last)
diff = %x{ git diff-index --cached --name-only #{old_sha} 2> /dev/null }
#puts diff.inspect