Skip to content

Instantly share code, notes, and snippets.

View djanowski's full-sized avatar

Damian Janowski djanowski

View GitHub Profile
@djanowski
djanowski / missing-requires.sh
Created April 14, 2014 19:38
Find your missing requires in shotgun.rb
everywhere=$(ag '^\s*require' -w --ruby --no-numbers --ignore=test/ --ignore=tasks/ --ignore=scripts/ | awk -F: '{gsub(/^ */, "", $2); print $2}' | sort | uniq)
shotgun=$(ag '^\s*require' -w --no-numbers shotgun.rb)
echo "$everywhere" | grep -v -f <(echo "$shotgun")
require "benchmark"
$baz = nil
class Foo
def self.foo
@foo || $baz
end
end
require "benchmark"
COUNT = 1_000_000
@status = 200
Benchmark.bm(20) do |x|
x.report("noop") do
COUNT.times { }
end
@djanowski
djanowski / gist:6826538
Created October 4, 2013 14:10
Print tree by level
Node = Struct.new(:value, :children)
tree = Node.new(
1,
[
Node.new(
2,
[Node.new(5, []), Node.new(6, [])]
),
@djanowski
djanowski / vim-install
Created August 27, 2013 02:44
vim-install(1) -- Vim plugin installer for the rest of us.
#!/bin/bash
usage() {
cat >&2 <<EOS
Usage: vim-install <Git URL>
EOS
exit 1
}
@djanowski
djanowski / autoload_killer.rb
Created August 16, 2013 21:04
AutoloadKiller middleware. Helps you debug race conditions due to misuse of Kernel#autoload.
# If some requests in your web application fail but subsequent requests don't,
# you may be a victim of Kernel#autoload.
#
# As you know, Kernel#autoload is not thread-safe. Moreover, missing constants
# trigger requiring other files, which can some times do more than just provide
# that missing constant. Yet many libraries still use it:
# https://github.com/search?l=ruby&q=autoload+language%3Aruby&type=Code
#
# So if you think you're having any of these issues, this middleware should be
# able to provide a hint as to which files are the ones you should be requiring
@djanowski
djanowski / composition.rb
Created August 16, 2013 15:14
Cuba composition and status codes
require File.expand_path("helper", File.dirname(__FILE__))
setup do
users = Cuba.new do
on get do
on "foo" do
res.write "foo"
end
on "bar" do
@djanowski
djanowski / ohm-pooled.rb
Last active December 21, 2015 03:39
How to use Redis::Pool with Ohm.
require "redis/pool"
require "ohm"
# The current version of Ohm (1.3.2) does not use Redis.current, so we need to
# monkey patch or create a base class for models.
class Ohm::Model
def self.db
Redis.current
end
end
require "cuba"
module Cuba::Routable
def self.setup(app)
app.define do
self.class.routes.each do |route, app|
on(route) { run(app) }
end
end
end
@djanowski
djanowski / each_memory.rb
Created April 12, 2013 22:50
Loop over 100k Ohm instances to measure memory and time consumption.
require_relative "benchmark"
require "ffaker"
require "batch"
redis = Redis.connect
until (begin; redis.ping; rescue; nil; end); end
class Post < Ohm::Model