Skip to content

Instantly share code, notes, and snippets.

begin
class_to_load = class_name.constantize
class_to_load.new
# ...
rescue NameError => e
# do other things if class_name is not an actual class
end
@zev
zev / active_record_gem_3_2.rb
Created June 18, 2014 02:59
RecordNotFound missing descriptive message from various ActiveRecord versions
# Activate the gem you are reporting the issue against.
gem 'activerecord', '3.2.18'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
@zev
zev / github_show.sh
Created September 18, 2012 03:55 — forked from andrewplummer/github_show.sh
Open a link to a file in Github from the command line.
#!bin/bash
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
parse_git_remote() {
git remote -v | head -n 1 | sed 's/.*github.com:\(.*\).git .*/\1/'
}
@zev
zev / dbtap.clj
Created May 16, 2012 02:13
Custom Cascalog tap from a db query
(defn convert-rs
[rs keys]
(vec (doall
(map #(vec (map-values % keys)) rs))))
(defn map-values
[r keys]
(map #(% r) keys))
(defn users-query
@zev
zev / DWIMlogging
Created September 28, 2011 03:03
Buffered logging to handle encoding conversion failures
def flush
@guard.synchronize do
unless buffer.empty?
old_buffer = buffer
begin
@log.write(old_buffer.join)
rescue Encoding::UndefinedConversionError
puts "Enc err"
@log.write(old_buffer.map { |b| b.force_encoding(Encoding.default_internal) }.join)
puts "Enc Done"
@zev
zev / gist:1196530
Created September 6, 2011 03:49
programming-praxis remove dup chars
;; solution for part 1 http://programmingpraxis.com/2011/09/02/two-string-exercises/
(defun rdup (str)
(defun rdup_ (str res)
(if (null str)
res
(if (member (car str) res)
(rdup_ (cdr str) res)
(rdup_ (cdr str) (append res (list (car str)))))))
(concat (rdup_ (string-to-list str) '())))
@zev
zev / example_aws_s3_digest_usage.txt
Created September 3, 2010 10:21
How to send the hexdigest safely in s3
S3Object.store('lambda.jpg', d, 'mybucket', :content_type => 'image/jpeg', :content_md5 => Base64.encode64(Digest::MD5.digest(d)))
@zev
zev / *scratch*
Created September 1, 2010 09:08
sample conditional shell checks
# rough if else ...
execute "restarting-mysql-with-custom-config" do
command "/etc/init.d/mysql restart"
only_if "pgrep mysqld"
end
execute "starting-mysql-with-custom-config" do
command "/etc/init.d/mysql start"
not_if "pgrep mysqld"
;; Fix indentation for buffer
(defun fix-read-only-indentation ()
"Fix the indentation of a read only file. For now you have to
remember to not save this change if you have write access to the
file"
(interactive)
(toggle-read-only)
(indent-region (point-min) (point-max)))