Skip to content

Instantly share code, notes, and snippets.

View jakeonrails's full-sized avatar

Jake Moffatt jakeonrails

View GitHub Profile
@jakeonrails
jakeonrails / benchmark.rb
Created November 6, 2012 23:15 — forked from panthomakos/benchmark.rb
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,
]
describe '#game_won?' do
winning = [
<<-BOARD
.x...
ox...
oxo..
oxo..
BOARD
]
# Assuming all the methods called here also return true/false,
# use the logical composition operators to create a final result
# which can short-circuit if one of the methods fails:
def composed_method
frobulate_widgets and refrobulate_widgets and confribulate_frobulations
end
@jakeonrails
jakeonrails / ping_pong.rb
Created February 27, 2013 20:08
PING PONG ENUMERATOR!
pinger = Enumerator.new do |yielder|
loop
yielder << "PING"
end
end
pinger.each do |ping|
puts ping
puts "PONG"
end
@jakeonrails
jakeonrails / fix_newrelic_pow.md
Created April 11, 2013 21:12
Newrelic developer mode returns "no route matches" when you hit /newrelic and you happen to be running pow.
@jakeonrails
jakeonrails / json_api_dsl.rb
Created August 13, 2013 03:24
JSON API test DSL
describe 'POST api/v1/sessions' do
def self.request(options)
let(:api_response) {
send(
options[:method].downcase,
options[:path],
options[:json],
)
last_response
}
@jakeonrails
jakeonrails / parser_spec.rb
Created January 25, 2014 00:38
Reverse Polish Notation parser
require 'spec_helper'
class Parser
def parse(string)
stack = []
tokens = string.split(' ')
tokens.each do |token|
if token =~ /\d/
value = token.to_i
@jakeonrails
jakeonrails / database_url.rb
Created May 22, 2012 07:01
Parse heroku DATABASE_URL
<%
require 'cgi'
require 'uri'
begin
uri = URI.parse(ENV["DATABASE_URL"])
rescue URI::InvalidURIError
raise "Invalid DATABASE_URL"
end
class RingBuffer
def initialize(redis=nil, list_key=nil)
@redis = redis
end
def next
JSON(@redis.rpoplpush(@list_key, @list_key))[0]
end
def add(item)
require 'awesome_print'
# Put this file into your spec/support directory in order to have RSpec automatically report
# on the total number of database records created during a run of your test suite.
class ActiveRecordInsertCountReport
include Singleton
def subscribe_to_notifications
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
name, start, ending, transaction_id, payload = args
table_name = payload[:sql][/insert into "(.+?)"/i, 1]