Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dbi
dbi / .gitconfig
Created November 29, 2012 09:57
Giticonfig
[user]
name = David Billskog
email = billskog@gmail.com
[color]
diff = true
status = auto
branch = auto
interactive = auto
ui = true
[core]
@dbi
dbi / deep_struct.rb
Created November 20, 2012 09:03
Make a hash with keys callable as methods
class DeepStruct
attr_reader :hash
def initialize(hash)
@hash = hash.with_indifferent_access
end
def method_missing(method, *args, &block)
if hash.key?(method)
value = hash[method]
@dbi
dbi / time_logger.rb
Created October 3, 2012 13:06
Log duration of method calls
module TimeLogger
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
attr_writer :time_logger
def time_logger
@time_logger ||= Logger.new(File.join(Rails.root, 'log', 'time_logger.log'))
@dbi
dbi / .bash_profile
Created September 6, 2012 14:03
Hitch description in your prompt
function branch {
ref=$(git symbolic-ref HEAD 2> /dev/null)
if [ $? -eq 0 ]; then
echo " "${ref#refs/heads/}""
fi
}
function hitch_pair {
source ~/.hitch_export_authors
if [ `git symbolic-ref HEAD 2> /dev/null` ] \
@dbi
dbi / model_cleanup.rb
Created August 14, 2012 12:46
Detect invalid activerecord models in your app
# Disable logging
ActiveRecord::Base.logger = Logger.new(nil)
# Make sure all models are loaded
Dir.glob("app/models/*.rb").each do |f|
begin
require File.basename(f).sub(".rb", "")
rescue
puts "could not require #{f}"
end
@dbi
dbi / method_sniffer.rb
Created May 9, 2012 13:36
Sniffing for Array#to_s usage before upgrading to ruby 1.9 (where the behaviour changes)
# Log Array#to_s to make sure we are not using it and expeting the Ruby 1.8.x behaviour
class Array
alias :_to_s :to_s
def to_s(*args)
Rails.logger.warn "Was calling Array#to_s from #{caller.first.split(":in").first}"
_to_s(*args)
end
end
@dbi
dbi / current.rb
Created March 1, 2012 12:44
Ruby current line
def current_line
caller.first.split(":in").first
end
puts current_line # current.rb:5
@dbi
dbi / edit.html.mustache
Created May 4, 2011 09:31
Rails form helper for mustache spike/proof of concept
<!-- A scaffolded edit view converted to mustache -->
<h1>Editing post</h1>
{{#form}}
{{#errors?}}
<div id="error_explanation">
<h2>{{error_header}}</h2>
</div>
<ul>
@dbi
dbi / Ruby
Created November 2, 2010 20:38
Random string
rand(32**8).to_s(32) # => "kff09q5l"