This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unless defined? Stubber | |
module Stubber | |
METHOD_SAFE = Hash.new { |h,k| h[k] = Hash.new } | |
def stub_method(klass, method_name, &stub) | |
METHOD_SAFE[klass][method_name] ||= klass.instance_method(method_name) | |
klass.class_eval { define_method(method_name, &stub) } | |
end | |
def unstub_method(klass, method_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# change postgresql template1 db encoding to utf8 (unicode) by dropping it and recreating from template0 | |
UPDATE pg_database SET datistemplate=false WHERE datname='template1'; | |
drop database template1; | |
create database template1 with template = template0 encoding = 'UTF8'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Module | |
def define_method_in_ancestor(name, &block) | |
include Module.new { define_method(name, &block) } | |
end | |
def eval_in_ancestor(*args) | |
include Module.new { eval(*args) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Spec::Matchers.define :be_same_date_as do |expected| | |
match do |actual| | |
(actual.to_date - expected.to_date).to_f < 1.0 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Factory girl, relaxed. | |
# | |
# Factory.define :user do |f| | |
# f.login 'johndoe%d' # Sequence. | |
# f.email '%{login}@example.com' # Interpolate. | |
# f.password f.password_confirmation('foobar') # Chain. | |
# end | |
# | |
# Factory.define :post do |f| | |
# f.user { Factory :user } # Blocks, if you must. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# More "proper" than a miniskirt (http://gist.github.com/273579). | |
class Minidress | |
@@factories = {} | |
class << self | |
def define(name, &block) | |
@@factories[name.to_s] = block | |
end | |
def build(name, attrs = {}) | |
new(name.to_s.classify.constantize.new, &@@factories[name.to_s]).record |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__ruby_version() { | |
echo `ruby -v | sed -e "s/ (.*//g"` | |
} | |
PS1=" ... \$(__ruby_version) ... " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/ruby | |
# | |
# USAGE: run the server, then GET a url like this one: | |
# | |
# curl localhost:7892/This+is+the+title/This+is+the+body | |
# | |
require 'rubygems' | |
require 'eventmachine' | |
require 'evma_httpserver' | |
require 'libnotify' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'nokogiri' | |
(Nokogiri::HTML.parse(open('http://installed-gems.heroku.com')) / "li").each do |e| | |
_, gem, versions = *e.text.match(/([^ ]+) \(([^\)]+)\)/).to_a | |
versions = versions.split(", ") | |
puts "gem install --ignore-dependencies %s -v %s" % [gem, versions.first] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'zlib' | |
require 'digest/sha2' | |
require 'benchmark' | |
FILES = Dir["*dat"] | |
Benchmark.bm do |rep| | |
rep.report("adler32") {FILES.each { |f| Zlib.adler32(File.read(f))} } | |
rep.report("crc 32") {FILES.each { |f| Zlib.crc32(File.read(f))} } | |
rep.report("sha 2 ") {FILES.each { |f| Digest::SHA2.digest(File.read(f))} } |