Skip to content

Instantly share code, notes, and snippets.

View EmmanuelOga's full-sized avatar
🕹️

Emmanuel Oga EmmanuelOga

🕹️
View GitHub Profile
@EmmanuelOga
EmmanuelOga / stubber.rb
Created January 6, 2010 12:05
simple ruby method stubber for all instances of a class
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)
@EmmanuelOga
EmmanuelOga / pgsql_change_template_1.sql
Created January 6, 2010 13:21
change postgresql template1 db encoding to utf8 (unicode) by dropping it and recreating from template0
# 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';
@EmmanuelOga
EmmanuelOga / super_powers.rb
Created January 8, 2010 06:50
Retain your "super" powers :)
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
@EmmanuelOga
EmmanuelOga / be_same_date.rb
Created January 11, 2010 19:17
rspec be_same_date_as matcher
Spec::Matchers.define :be_same_date_as do |expected|
match do |actual|
(actual.to_date - expected.to_date).to_f < 1.0
end
end
# 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.
# 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
@EmmanuelOga
EmmanuelOga / bash_prompt_helper.sh
Created January 19, 2010 19:22
Ruby version on the bash prompt
__ruby_version() {
echo `ruby -v | sed -e "s/ (.*//g"`
}
PS1=" ... \$(__ruby_version) ... "
@EmmanuelOga
EmmanuelOga / notifserver.rb
Created January 26, 2010 05:32
Http to libnotify gateway
#/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'
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
@EmmanuelOga
EmmanuelOga / checksums_benchmark.rb
Created February 3, 2010 23:22
Ruby checksums benchmark
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))} }