Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / Gemfile
Created August 8, 2019 04:50
suggest-typed and todo hang
gem 'sorbet'
@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 / 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 / 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 / Gemfile
Created January 10, 2017 01:27
Hanami Model Primary Key Exception
source 'https://rubygems.org'
gem 'sqlite3'
gem 'hanami-model'
@panthomakos
panthomakos / Gemfile
Created December 13, 2016 23:48
mapping (Hanami::Model::Error)
source 'https://rubygems.org'
gem 'sqlite3'
gem 'hanami-model'
@panthomakos
panthomakos / Gemfile
Created October 18, 2012 18:30
Guard-Yard Example
source "http://rubygems.org"
gem 'guard-yard'
gem 'rb-fsevent', :require => false
@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)")