Skip to content

Instantly share code, notes, and snippets.

@jonhinson
jonhinson / monty.rb
Created October 14, 2011 22:58
Demonstration of the Monty Hall problem and that a switching strategy is always better ( you win 2/3 of the time).
class Monty
STAY = true
SWITCH = false
def initialize(strat = STAY)
@strategy = strat
@times_won = 0
end
def play(n)
@jonhinson
jonhinson / gist:1229453
Created September 20, 2011 15:43
When using resque in conjunction with PostgreSQL (I am deploying on Heroku's cedar stack), make sure you have this before_fork hook
task "resque:setup" => :environment do
Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
end
@jonhinson
jonhinson / mongdb_import.rb
Created May 9, 2011 18:54
Import data into mongodb from json file
`mongoimport --host #{host} --db #{db} --collection #{collection} --type #{type} --file #{file} --upsert --upsertFields #{upsertFields}`
@jonhinson
jonhinson / mongodb_listings_per_city.js
Created May 8, 2011 19:50
Count number of listings per city in a given state.
db.$cmd.findOne({group : {
ns : "listings",
cond : { state: "TX"},
key : {state : 1, city_slug: 1},
initial : {count : 0},
$reduce : function(obj,prev){prev.count++;}}})
# working on testing loops like the one below
# describe Foo do
# it "should write hi" do
# Bar.stubs(:create)
# Delayed::Job.stubs(:enqueue)
# Bar.stubs(:first).returns(Bar.new, nil)
# Foo.any_instance.stubs(:sleep)
# foo = Foo.new
# foo.should_receive(:puts).with("hi")
# foo.should_receive(:sleep).with(1)
default = {
"key1" => "val1",
"key2" => "val2",
"key3" => {
"1" => "one",
"2" => "two"
},
"key4" => "four"
}
custom = {
$(document).ready(function() {
setTimeout(function() {
$('#april_fools').css('-moz-transform', 'rotate(0deg)');
}, 5000)
)};
agent = WWW::Mechanize.new
agent.user_agent_alias = 'Mac FireFox'
page = agent.get('https://www.linkedin.com/secure/login')
page = page.form_with(:name => 'login') do |f|
f.session_key = "example@example.com"
f.session_password = "notreal"
end.submit
page = agent.get('http://www.linkedin.com/home')
jQuery(function() {
$.cookie('tz', (new Date()).getTimezoneOffset());
});
module ActiveRecord
module Associations
module ClassMethods
private
@@valid_keys_for_belongs_to_association = [
:class_name, :primary_key, :foreign_key, :foreign_type, :remote, :select, :conditions,
:include, :dependent, :counter_cache, :extend, :polymorphic, :readonly, :joins, # added :joins key
:validate, :touch
]
@@valid_keys_for_has_many_association = [