Skip to content

Instantly share code, notes, and snippets.

View jimeh's full-sized avatar

Jim Myhrberg jimeh

View GitHub Profile
@jimeh
jimeh / weight.rb
Created July 16, 2009 07:14
weight.rb (RoR): Lets you pick a row from a table randomly based on the rows weight.
#
#
# weight.rb (A Ruby on Rails Model).
# Lets you pick a row from a table randomly based on the rows weight.
#
# Your table will need three columns:
# - weight (float)
# - weight_begin (float)
# - weight_end (float)
#
#
# The dumbest piece of code I've EVER written.
#
# I REALLY don't know WTF I was thinking, or not thinking.
# I must have been drunk and asleep while I wrote this method.
#
# It's not only stupid, but IT DOESN'T EVEN WORK! *stabs self in face*
#
def stupid_method
@jimeh
jimeh / mass_insert_mongo.js
Created September 20, 2009 00:44
Simple MySQL vs. MongoDB Insert benchmark.
for (var i=0; i < 100000; i++) {
db.mass_insert.save({
"first_name": "James",
"middle_name": "Richard",
"last_name": "Windsgate",
"email": "[email protected]"
});
};
@jimeh
jimeh / call_parallel.rb
Created October 7, 2009 13:54
Ruby: #call_parallel
#
# Run multiple system commands in parallel
#
def call_parallel(cmds = [], check_interval = nil)
if cmds.size > 0
threads, results = [], []
cmds.each do |cmd|
threads << Thread.new { results << [cmd, `#{cmd}`, $?] }
end
is_done = false
# Dump database "hello_world"
$ ./dump_to_s3.rb hello_world
dumping hello_world database...
uploading /mnt/hello_world-2009-10-30_12-11-44.sql_aa to S3.
uploading /mnt/hello_world-2009-10-30_12-11-44.sql_ab to S3.
uploading /mnt/hello_world-2009-10-30_12-11-44.sql_ac to S3.
@jimeh
jimeh / _NOTE.md
Created November 16, 2009 00:16
My Git related config and aliases

NOTE

This gist is now deprecated as I've finally sorted out and made my dotfiles public.

My ~/.gitconfig is available here, and my git-related shell setup here.

@jimeh
jimeh / sudome.rb
Created November 20, 2009 23:30
sudome — A simple Ruby method which checks if the current script is running as root, and if not, re-invokes itself by using the sudo command.
# A simple Ruby method which checks if the current script
# is running as root, and if not, re-invokes itself by
# using the sudo command.
def sudome
if ENV["USER"] != "root"
exec("sudo #{ENV['_']} #{ARGV.join(' ')}")
end
end
require "rubygems"
require "friendly"
require "models/user"
Friendly.configure(
:adapter => "mysql",
:host => "localhost",
:user => "root",
:password => "boot",
:database => "friendlyplay"
@jimeh
jimeh / .gitignore
Created December 24, 2009 11:51
A quick head-to-head performance test of JSON vs. BSON.
.DS_Store
data.rb
results.txt
@jimeh
jimeh / latin1-to-utf8.rb
Created December 29, 2009 14:58
Convert a MySQL database from latin1 to utf8 charset.
#! /usr/bin/env ruby
#
# Convert a MySQL database from latin1 to utf8 charset.
#
# Based on the short but slightly limited shell script in this article:
# http://yoonkit.blogspot.com/2006/03/mysql-charset-from-latin1-to-utf8.html
#
# Use at your own risk! I take not responsiblity for anything or
# anyone that might be damaged, lost, or killed from using this