Skip to content

Instantly share code, notes, and snippets.

View sco's full-sized avatar

Scott Raymond sco

View GitHub Profile
I am +sco on OneName. #verifymyonename

Keybase proof

I hereby claim:

  • I am sco on github.
  • I am sco (https://keybase.io/sco) on keybase.
  • I have a public key whose fingerprint is CE48 F2FE C915 5969 54AF 683D 3D11 FC7E E1D8 B758

To claim this, I am signing this object:

@sco
sco / gist:12089
Created September 22, 2008 18:32
class Array
def weighted_random(weight_method=:weight)
return nil if empty? # return nil if there's no elements
elements = sort_by { Kernel.rand } # shuffle the elements.
weights = elements.map(&weight_method) # get the weight for each element (e.g.: 5, 10, 2, 0, 1, 5).
point = Kernel.rand * weights.sum # pick a random point between zero and the sum of the weights.
elements.zip(weights).each do |e, weight| # walk through each element,
return e if weight >= point # and if its weight is >= the point, return it
point -= weight # otherwise subtract the element's weight from the point
@sco
sco / gist:12334
Created September 23, 2008 16:56
require 'timeout'
class RequestTimeout < Merb::Rack::Middleware
def initialize(app, seconds)
super(app)
@seconds = seconds
end
def deferred?(env)
@sco
sco / couchdb.yml
Created October 9, 2008 18:17
initializing couchrest in a merb app
---
development: &defaults
:url: http://0.0.0.0:5984/gathering
production: &production
<<: *defaults
:url: http://0.0.0.0:5984/gathering
@sco
sco / rack.rb
Created December 9, 2008 17:53
odds_server = Proc.new do |env|
if env['PATH_INFO'] == '/odds'
[200, {'Content-Type'=>'application/json'}, '{claim_code:"123-sdfjhsdf"}']
else
[404, {}, '']
end
end
run Rack::Cascade.new([odds_server, Merb::Rack::Application.new])
@sco
sco / jw-nav.html
Created January 31, 2009 21:37
minimal version of jacobswellchurch.org nav markup & styles
<html>
<head>
<title>JW Nav</title>
<style>
/* reset margins and padding on every element */
* { margin: 0; padding: 0; }
/* style the whole page */
require 'net/http'
require 'rubygems'
require 'json'
# Mimics the interface of RightAws::SdbInterface, using CouchDB for persistence.
#
# FS = FakeSimpleDB.new('myapp')
# FS.create_domain('mydb')
# FS.put_attributes('mydb', 'myitem', { 'Jon' => ['foo', 'bar'] })
# FS.get_attributes('mydb', 'myitem')
@interface CustomNavigationBar : UINavigationBar {
UIImageView *backgroundView;
}
@end
@implementation CustomNavigationBar
- (void)setBackgroundImage:(UIImage*)image {
if(image == NULL){
return;
class WebHook < ActiveRecord::Base
class << self
# Pushes a job onto a queue
def visit!(visit)
JobQueue.new(:events).push({
:type => 'visit',
:visit_id => visit.id,
:user_id => visit.user_id,