Skip to content

Instantly share code, notes, and snippets.

View ColinDKelley's full-sized avatar

Colin Kelley ColinDKelley

  • Invoca, Inc.
  • Santa Barbara, CA
View GitHub Profile
@ColinDKelley
ColinDKelley / lwq-example.rb
Created March 13, 2014 00:07
limited_work_queue-example.rb
queue = LimitedWorkQueue(EventMachine, 2)
messages.each do |message|
queue.add do
message.send # no more than 2 of these will run at the same time
end
end
require 'rubygems'
#require 'rubygems/user_interaction' # Required with some older RubyGems
require 'isolate/now'
require 'eventmachine'
require 'em-websocket'
require 'em-http'
require 'nokogiri'
require 'json'
require 'pp'
@ColinDKelley
ColinDKelley / hash_benchmark.rb
Last active November 5, 2016 17:16
hash benchmark
# -*- immutable: string -*-
require 'benchmark'
class Request
def initialize(first, last, city, state, country)
@hash =
{
'first' => first,
'last' => last,
Without magic comment: 3.93 seconds
With magic comment: 1.93 seconds
2X faster!
@ColinDKelley
ColinDKelley / gist:5298850
Last active December 15, 2015 17:49
Script to compare failing Rails 3 tests cases across builds
build_numbers = [4036, 4037, 4041, 4044, 4045, 4048]
build_stats = build_numbers.map { |i| YAML.load_file("../shared/builds/#{i}/test_stats.yml") }
failing = build_stats.map do |run|
run.reduce([]) do |a, t|
t.last.each { |test_case, attrs| a << [t.first, test_case] if attrs.is_a?(Hash) && attrs['last_result'] != 'success' }
a
end
end
all_failing = failing.inject(&:|).sort
sometimes_failing = all_failing - failing.inject(&:&)
@ColinDKelley
ColinDKelley / min_max.rb
Created September 24, 2012 16:28
Math.min and Math.max
module Math
def self.min *args
args.min
end
def self.max *args
args.max
end
end
@ColinDKelley
ColinDKelley / elevation.rb
Created September 24, 2012 15:59
elevation
class Elevation
attr_reader :elevations
def initialize(elevations)
@elevations = elevations
highest = 0
@highest_lefts = @elevations.map { |elevation|
highest = Math.max(highest, elevation)
}
@ColinDKelley
ColinDKelley / gist:3702480
Created September 11, 2012 22:06
rr_monitoring
module RRMonitoring
class Statistic
STATS_HOST = "stats.ringrevenue.net"
STATS_PORT = 8125
def timing(name, &blk)
start = Time.now
yield
finish = Time.now
@ColinDKelley
ColinDKelley / gist:2927004
Created June 13, 2012 22:48
Hobo example
class User < ActiveRecord::Base
fields do
first_name :string, :ruby_default => 'First'
last_name :string :ruby_default => 'Last', :null => false, :default => ''
name_addr :string, :ruby_default => lambda { "#{full_name} <#{email}>" }
email :string :ruby_default => lambda { organization.default_email }
level :enum, :limit => [:Basic, :Admin, :Super], :default => :Basic, :null => false
end
def full_name
class User < ActiveRecord::Base
attr_default :first_name, 'First'
attr_default :last_name, 'Last'
attr_default :name_addr, lambda { "#{full_name} <#{email}>" }
attr_default :email, lambda { organization.default_email }
def full_name
[first_name, middle_name, last_name].select { |name| !name.blank? } * ' '
end