Skip to content

Instantly share code, notes, and snippets.

View rapimo's full-sized avatar

Manuel Kniep rapimo

View GitHub Profile
@rapimo
rapimo / cleanup.sh
Created December 28, 2011 17:18 — forked from mdp/cleanup.sh
Git branch cleanup
# This must be run from master
git checkout master
# Update our list of remotes
git fetch
git remote prune origin
# Remove local fully merged branches
git branch --merged master | grep -v 'master$' | xargs git branch -d
# Show remote fully merged branches
echo "The following remote branches are fully merged and will be removed:"
git branch -r --merged master | sed 's/ *origin\///' | grep -v 'master$'
@rapimo
rapimo / connection_fix.rb
Created January 23, 2012 08:47 — forked from mauricioszabo/connection_fix.rb
MySQL/Postgres server has gone away fix
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
@rapimo
rapimo / rails_start.scpt
Created February 21, 2012 17:18
iterm2 AppleScript for my Rails Project
-- ~/Library/Application\ Support/iTerm/Scripts/setup_rails.scpt
-- Thanks to http://www.worldgoneweb.com/2011/iterm2-advanced-features/#comment-20560
-- http://www.iterm2.com/#/section/documentation/scripting
tell application "iTerm"
activate
set myterm to (make new terminal)
set cd_to_my_project_path to ("cd ~/Projekte/my_awesome_project")
-- you can altenativly tell the first terminal
tell the first terminal
launch session "Panes"
@rapimo
rapimo / print_mail
Created March 12, 2012 14:27
print email addresses sent out from logfile
cat /var/log/mail.log | grep "status=sent" | awk '{print $7}' | awk '{ sub(/^to=</, ""); print }' | awk '{ sub(/>,$/, ""); print }'
@rapimo
rapimo / cracker.rb
Created May 8, 2012 07:19
crackme solution in ruby
require "benchmark"
class Cracker
class << self
## runs the code retuning sek runtime
def run(test)
Benchmark.realtime{`./crackme #{test}`}
end
@rapimo
rapimo / array_add.rb
Created July 4, 2012 09:32
add two arrays
class Array
# [1,2,3].add [1,2,3] => [2, 4, 6]
def add (other)
self.zip(other).map{|a| a.inject(0){|s,n| s+=n || 0;s }}
end
end
@rapimo
rapimo / gist:3182134
Created July 26, 2012 13:52
change ENCODING for template1
-- Postgres makes it difficult four you to shoot yourselves in the foot
-- So it is not allowed to delete template databases
UPDATE pg_database SET datistemplate = FALSE where datname = 'template1';
DROP DATABASE template1;
CREATE DATABASE template1 WITH template = template0 encoding = 'utf8';
UPDATE pg_database SET datistemplate = TRUE where datname = 'template1';
@rapimo
rapimo / gist:3183297
Created July 26, 2012 17:20
Find useless indexes on postres
SELECT idstat.relname AS table_name,
indexrelname AS index_name,
idstat.idx_scan AS times_used,
pg_size_pretty(pg_relation_size(idstat.relname::regclass)) AS table_size, pg_size_pretty(pg_relation_size(indexrelname::regclass)) AS index_size,
n_tup_upd + n_tup_ins + n_tup_del as num_writes,
indexdef AS definition
FROM pg_stat_user_indexes AS idstat JOIN pg_indexes ON indexrelname = indexname
JOIN pg_stat_user_tables AS tabstat ON idstat.relname = tabstat.relname
WHERE idstat.idx_scan < 200
AND indexdef !~* 'unique'
@rapimo
rapimo / gist:3250341
Created August 3, 2012 18:44
count occurrences of array values in postgres using hstore
SELECT hstore(array_agg(v), array_agg(c::text)) FROM (
SELECT v, COUNT(*) as c ,1 as agg from unnest(ARRAY['foo','bar','baz','foo']) v GROUP BY v) t
GROUP BY agg
--> "bar"=>"1", "baz"=>"1", "foo"=>"2"
@rapimo
rapimo / ar_null_object.rb
Created August 7, 2012 11:02
implementation for the NullObject Pattern in ActiveRecord
module ActiveRecord
module NullObject #:nodoc:
module InstanceMethods
# should quark like a nil
def nil?
true
end
def present?
false