Skip to content

Instantly share code, notes, and snippets.

@pithyless
pithyless / check_for_n1_queries.rb
Last active May 5, 2016 12:57
Helper to check for N+1 queries in rspec
require 'method_struct'
class CheckForN1Queries < MethodStruct.new(:generator, :operation, :debug)
IGNORED_SQL = [
/^SHOW/, /^EXPLAIN/, /^PRAGMA (?!(table_info))/, /^SELECT currval/, /^SELECT CAST/,
/^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/,
/^RELEASE SAVEPOINT/, /^SHOW max_identifier_length/
]
def call
@pithyless
pithyless / gist:1547905
Created January 1, 2012 17:52
Git track upstream repo
One time:
git remote add upstream git@github.com:foo/bar.git
git fetch upstream
git checkout -b upstream upstream/master
Each time you want to update:
git checkout upstream
git pull
@pithyless
pithyless / latency.markdown
Created April 6, 2016 10:12 — forked from iaintshine/latency.markdown
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@pithyless
pithyless / char_converter.rb
Created September 5, 2012 16:00
Rails middleware to encode characters and avoid exploding controllers
# config/initializers/char_converter.rb
require 'uri'
module Support
class CharConverter
SANITIZE_ENV_KEYS = [
"HTTP_COOKIE", # bad cookie encodings kill rack: https://github.com/rack/rack/issues/225
"HTTP_REFERER",
"PATH_INFO",
@pithyless
pithyless / algorithm.txt
Last active December 29, 2015 00:58 — forked from adamkal/parser.py
0123 0123 0123 0123 0123 0123 0123 0123 0123 0123
1 1 _ 1 _ 1 1 _ 1 _ 1 _ 1 _ 1 _ 1 _
2 | 2 _| 2 _| 2|_| 2|_ 2|_ 2 | 2|_| 2|_| 2| |
3 | 3|_ 3 _| 3 | 3 _| 3|_| 3 | 3|_| 3 _| 3|_|
000101100
+4 +7 -5 +6
0 - 2 4 6789
@pithyless
pithyless / equalable.rb
Created April 12, 2013 18:56
Equalable ruby mixin.
module Equalable
def ==(o)
o.class == self.class && o.equality_state == equality_state
end
alias_method :eql?, :==
def hash
equality_state.hash
end
end
@pithyless
pithyless / custom_assertions.rb
Created July 23, 2012 12:33
minitest samples
# https://gist.github.com/2032303
module MiniTest::Assertions
def assert_palindrome(string)
assert string == string.reverse, "Expected #{string} to read the same way backwards and forwards"
end
def assert_default(hash, default_value)
assert default_value == hash.default, "Expected #{default_value} to be default value for hash but was #{hash.default.inspect}"
end
@pithyless
pithyless / gazebo.rb
Created April 1, 2012 16:04
DCI via SimpleDelegator and a hint of metaprogramming
get '/give/me/gazebo/not/gazelle/:min_size' do
pricing = PriceCalculator.new
ParamsExtractor.new(pricing).parse(params)
GazelleFinder.new(pricing).find_all_gazelles
GazelleFilter.new(pricing).remove_sick_gazelles # pricing has access to #gazelles
GazeboBuilder.new(pricing).calculate_size_of_gazebo
GazeboPricer.new(pricing).calculate_final_price # pricing no longer has #gazelles
@pithyless
pithyless / group_of_thingies.rb
Created February 8, 2012 08:40 — forked from Peeja/group_of_thingies.rb
Test a block in Ruby
class GroupOfThingies
attr_accessor :thingies
# Use like this:
#
# group_of_thingies = GroupOfThingies.new
# group_of_thingies.each do |thing|
# puts "Check out this awesome thing: #{thing}!"
# end
#
@pithyless
pithyless / match_method.rb
Created February 8, 2012 08:37 — forked from avdi/match_method.rb
Defining method_missing and respond_to? in one fell swoop
# Do you ever define #method_missing and forget #respond_to? I sure
# do. It would be nice if we could do them both at the same time.
module MatchMethodMacros
def match_method(matcher, &method_body)
mod = Module.new do
define_method(:method_missing) do |method_name, *args|
if matcher === method_name.to_s
instance_exec(method_name, *args, &method_body)
else