Skip to content

Instantly share code, notes, and snippets.

View jbarnette's full-sized avatar

John Barnette jbarnette

View GitHub Profile
# Run the given block +num+ times and then print out the mean, median, min,
# max, and stddev of the run. For example:
#
# irb> stats(10) { sleep(rand / 100) }
# mean: 5.99ms
# median: 6.76ms
# min: 1.49ms
# max: 9.28ms
# stddev: 2.54ms
def stats(num)
@shinzui
shinzui / connection_fix.rb
Created December 6, 2009 06:18 — forked from defunkt/connection_fix.rb
MySQL automatic reconnect
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
~$ ARCHFLAGS='-arch i386 -arch x86_64'
~$ rvm install 1.8.7 --debug --reconfigure -C --enable-shared=yes
~$ wget http://sourceforge.net/projects/rubycocoa/files/RubyCocoa/1.0.0/RubyCocoa-1.0.0.tar.gz/download
~$ tar xzf RubyCocoa-1.0.0.tar.gz && rm RubyCocoa-1.0.0.tar.gz && cd RubyCocoa-1.0.0
~/RubyCocoa-1.0.0$ ruby install.rb config --build-universal=yes
~/RubyCocoa-1.0.0$ ruby install.rb setup
~/RubyCocoa-1.0.0$ sudo ruby install.rb install
(function() {
var ua = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3';
if (navigator.userAgent.indexOf(ua) === 0) {
document.write('<sc' + 'ript src="/js/ios3_bug.js"></sc' + 'ript>');
}
})();
// iPhone OS 3 requires an external script tag to be synchronously
// loaded in the HEAD od the HTML document if there's a reference
@jbarnette
jbarnette / example.rb
Created April 6, 2011 14:55
A naïve state machine for ActiveRecord. Ruby 1.9.
require "stateful"
class Folder < ActiveRecord::Base
include Stateful
# ...
stateful do
state :active
state :inactive
@cicloid
cicloid / ubuntu-11.10-gems.erb
Created October 28, 2011 22:49
Ubuntu 11.10 Chef Bootstrap with RBEnv
bash -c '
<% if knife_config[:bootstrap_proxy] -%>
(
cat <<'EOP'
<%= "proxy = #{knife_config[:bootstrap_proxy]}" %>
EOP
) > ~/.curlrc
<% end -%>
if [ ! -f /usr/bin/chef-client ]; then
@foca
foca / singleton.coffee
Created January 18, 2012 22:39
Small implementation of Singletons for Backbone
# Public: generates a mixable singleton implementation dependent on a model key.
# Once you mix it into a model, your model gains an .instance method that will
# generate an object and cache it. Further calls to the .instance methods will
# return the same object.
#
# key - The name of the attribute we use to index instances. Defaults to "id".
#
# Example
#
# # Called without arguments uses the "id" as key.
require 'minitest/unit'
require 'drb'
require 'rinda/tuplespace'
module MiniTest
class ParallelRunner < MiniTest::Unit
URI = 'druby://127.0.0.1:12345'
def _run_suites suites, type
hash = Hash[suites.map { |s| [s.name, s] }]
<textarea style="height: 100%; width: 100%; background: #222; padding: 5% 25%; color: #eee;"></textarea>
@drnic
drnic / snippet.rb
Created July 12, 2010 23:27
Sometimes you just need to know if anyone has forked your project recently
#!/usr/bin/env ruby
#
# USAGE:
#
# ruby -rubygems -ropen-uri -e 'eval open("http://gist.github.com/raw/473222/snippet.rb").read' jbarnette dr-nic-magic-awesome
#
# Or locally:
#
# $ show_forks jbarnette dr-nic-magic-awesome
# jbarnette - 2008/06/05 15:54:29 -0700