Skip to content

Instantly share code, notes, and snippets.

@panthomakos
panthomakos / Gemfile
Created May 18, 2011 18:41
Spork, RSpec, Sham and Caching Classes
source 'http://rubygems.org'
gem 'rails', '3.0.4'
group :test do
gem 'database_cleaner'
gem 'rspec-rails'
gem 'rspec-rails-matchers'
gem 'sham'
gem 'spork', '=0.9.0.rc7'
@panthomakos
panthomakos / example1.js
Created June 30, 2011 07:23
Avoiding Nested Callbacks in Javascript
var fs = require('fs');
fs.mkdir('./hello',0777,makeDirectory);
function makeDirectory(err){
if (err) throw err;
fs.writeFile('./hello/world.txt', 'Hello!', writeFile);
}
function writeFile(err){
@panthomakos
panthomakos / dynamic.1.8.7.rb
Created September 20, 2011 21:50
Dynamic Exception Rescue in Ruby
#!/usr/bin/env ruby
def match_message(regexp)
m = Module.new
(class << m; self; end).instance_eval do
define_method(:===) do |error|
regexp === error.message
end
end
m
@panthomakos
panthomakos / group.rb
Created September 20, 2011 22:57
Custom Error Messages in Ruby
class Group
module Error
class Standard < StandardError; end
class AlreadyAMember < Standard
def message
"You are already a member of this group."
end
end
@panthomakos
panthomakos / group.rb
Created September 20, 2011 23:09
Improved Custom Error Messages in Ruby
class Group
module Error
class Standard < StandardError; end
class AlreadyAMember < Standard; end
class NotPermittedToJoin < Standard; end
end
def join user
raise Error::NotPermittedToJoin unless self.permitted?(user)
raise Error::AlreadyAMember if self.member?(user)
@panthomakos
panthomakos / user_presenter_spec.rb
Created January 26, 2012 21:29
Simplified Version of user_presenter_spec.rb from DestroyAllSoftware
class Module
alias :let :define_method
end
UserPresenter = Struct.new(:user) do
let(:full_name){ [user.first_name, user.last_name].join(' ') }
end
describe UserPresenter do
let(:user){ stub(:user, :first_name => 'Bob', :last_name => 'Smith') }
@panthomakos
panthomakos / gil.rb
Created February 5, 2012 02:16
Ruby - Threading and the GIL
require 'benchmark'
require 'mysql2'
x = Mysql2::Client.new
y = Mysql2::Client.new
Benchmark.bm do |b|
b.report('w/o') do
x.query("SELECT SLEEP(1)")
y.query("SELECT SLEEP(1)")
@panthomakos
panthomakos / benchmark.rb
Created March 12, 2012 06:16
Rails ActiveSupport::Autoload Benchmark
# Rails 3.2 @ d5d241cb2c696f13e2c16efca0d24565a6e1c0a5
require 'active_support/dependencies/autoload'
require 'benchmark'
GC.disable
FILES = (1..1_000_000).map do |file|
["Base#{file}".to_sym, "base/#{file}"]
end
@panthomakos
panthomakos / benchmark.rb
Created May 3, 2012 20:06
Benchmark Your Bundle
#!/usr/bin/env ruby
require 'benchmark'
REGEXPS = [
/^no such file to load -- (.+)$/i,
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
/^Missing API definition file in (.+)$/i,
/^cannot load such file -- (.+)$/i,
]
@panthomakos
panthomakos / Gemfile
Created October 18, 2012 18:30
Guard-Yard Example
source "http://rubygems.org"
gem 'guard-yard'
gem 'rb-fsevent', :require => false