Skip to content

Instantly share code, notes, and snippets.

@pbyrne
pbyrne / performance.rb
Created September 27, 2012 16:28
Performance Comparison
# plain ActiveRecord
Benchmark.measure do
User.where(:activated => true).limit(200000).map(&:first_name)
end
# => 58.210000 12.970000 71.180000 ( 73.134415)
# writing the improved SQL by hand
Benchmark.measure do
ActiveRecord::Base.connection.select_all("select * from users where activated = 1 limit 200000").map { |attrs| attrs[:first_name] }
end
@pbyrne
pbyrne / gist:3692815
Created September 10, 2012 18:38
Ack! Performance!
# setup from controller and model code
params = {game_id: 3598462, play_by_play_string: true}
sort_order = [:play_index, :asc]
plays = Play.where(:game_id => params[:game_id]).order_by(sort_order)
plays = plays.first.class.prune_plays(plays)
methods = [:initial_screen]
play_by_play_string = params[:play_by_play_string]
if play_by_play_string
methods = plays.first.class.play_by_play_methods
end
@pbyrne
pbyrne / enablerebase.sh
Created August 23, 2012 20:20
Since Git's branch.enablerebase config option only works on branches created after enabling the setting, this iterates over your projects and their branches to enable it for existing branches.
#!/bin/bash
# Enables automatic rebase when pulling for each existing branch in your
# projects. This is useful in conjunction with the 'branch.enablerebase always'
# option, which enables this setting on new branches but leaves existing
# branches intact.
#
# Pass this a directory which contains all of your projects and it will enable
# thie option for all existing local branches in each Git repo within it.
@pbyrne
pbyrne / gist:3183751
Created July 26, 2012 18:46
first draft, untested, and doing terrible things with negative indexes
# desired_cluster is a hash of roles and their counts (e.g., {web: 2, redis: 1}
def equalize_servers fog, app, environment, desired_cluster
current_cluster = filtered_servers(fog, app, environment).group_by { |server| server.tags["role"] }
to_build = {}
to_destroy = []
current_cluster.each do |role, servers|
if desired_cluster.has_key? role
# we want some, but do we have the right amount?
difference = desired_cluster[role] - servers.count
@pbyrne
pbyrne / rspec-documentation.txt
Created March 18, 2012 20:51
RSpec --format=documentation Output
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
Automation::CLI
.perform(command)
passes the given command to the shell
.say(message)
displays the given message
.perform_in(dir, &block)
79c79
< let s:code = "print ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,})"
---
> let s:code = "print ($:)"
@pbyrne
pbyrne / setup.sh
Created October 6, 2011 20:50
Set Up Work Laptop
#!/bin/bash
echo "Updating Homebrew and bash completion"
brew update
echo "
# homebrew completion files for installed libraries
if [ -f `brew --prefix`/etc/bash_completion ]; then
. `brew --prefix`/etc/bash_completion
fi
" >> ~/.bash_profile
@pbyrne
pbyrne / comment.rb
Created September 5, 2011 01:49
Demonstrating Custom Validations to Prevent Specific Text
# Assumes a Comment model with a message attribute contianing the text of the
# comment. Change these to suit your needs.
class Comment
validate :disallow_phone_numbers_and_email_addresses
# possibly not the most performant, but it's clear
# assumes you have arrays of regexes you want to dissalow
def disallow_phone_numbers_and_email_addresses
if PHONE_NUMBERS.any? { |regex| message =~ regex }
errors.add(:message, "cannot contain phone numbers.")
@pbyrne
pbyrne / test
Created August 22, 2011 17:59
something
testing download
@pbyrne
pbyrne / sss
Created July 18, 2011 22:09
Simple script to perform SCM (svn, hg, git) commands across your workspace
#!/usr/bin/env ruby
#
# Simple script to perform SCM (svn, hg, git) commands across your workspace
# Edit the SHARED_WORKSPACE environment variable if your code isn't checked out to ~/workspace
# Skip to the end for the actual execution ... to keep it all in one file, had to define the class first
# Get the latest version from https://gist.github.com/1090808
USAGE = <<-end_usage
SSS performs SCM commands on all projects in your workspace. Set the
SHARED_WORKSPACE environment variable if your workspace is not ~/workspace.