Skip to content

Instantly share code, notes, and snippets.

View nottombrown's full-sized avatar

Tom B Brown nottombrown

View GitHub Profile
myState = "external state"
TestModel = Backbone.Model.extend
initialize: (options) ->
@myState = "internal state"
printState: =>
console.log @myState
$ ->
myState = "external state"
class TestModel extends Backbone.Model
initialize: (options) ->
@myState = "internal state"
printState: =>
console.log @myState
@nottombrown
nottombrown / README.markdown
Created March 5, 2012 01:29 — forked from Yavari/README.markdown
Bootstrap's Typeahead plugin extended (allowing for AJAX functionality) among other things

This is a fork of Bootstrap Typeahead that adds minimal but powerful extensions.

For example, process typeahead list asynchronously and return objects

  # This example does an AJAX lookup and is in CoffeeScript
  $('.typeahead').typeahead(
    # source can be a function
    source: (typeahead, query) ->
 # this function receives the typeahead object and the query string
-- Index hit rate
WITH idx_hit_rate as (
SELECT
relname as table_name,
n_live_tup,
round(100.0 * idx_scan / (seq_scan + idx_scan + 0.000001),2) as idx_hit_rate
FROM pg_stat_user_tables
ORDER BY n_live_tup DESC
),
class IndexUsersEmails < ActiveRecord::Migration
def self.up
execute "END"
add_pg_index :users, :email, :lock => false
execute "BEGIN"
end
end
def e(s, v=[:+, :-, :*, :/])v.empty? ? s.to_i : s.split(v[0].to_s).map{|y| e(y, v[1..-1])}.reduce(v[0])end
p e('2') # 2
p e('2 * 12') # 24
p e('1-10+4*2') # -1
p e('1+50/2*3') # 76
@nottombrown
nottombrown / sample.rb
Created August 15, 2013 03:05
Monkeypatch sample functionality into ActiveRecord
class ActiveRecord::Base
def self.sample(count=nil)
all_ids = (1..self.count).to_a
if count
ids = all_ids.sample(count)
self.where(id: ids)
else
id = all_ids.to_a.sample
self.find(id)
@nottombrown
nottombrown / solution.csv
Created January 3, 2014 04:52
Solution to the Grouper Cuttlefish Challenge
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
f_gender f_age f_height f_shoe_size* f_number_of_pets* f_platinum_albums* f_weekly_workouts* f_number_of_siblings* f_pokemon_collected* f_facebook_friends_count f_facebook_photos_count m_gender m_age m_height m_shoe_size* m_number_of_pets* m_platinum_albums* m_weekly_workouts* m_number_of_siblings* m_pokemon_collected* m_facebook_friends_count m_facebook_photos_count became_friends
female 27 67 5 7.5 7.5 8 7 7.5 333 457 male 25 72 5.5 8 7.5 8 7.5 7.5 884 601 FALSE
female 23 68 6.5 8 7.5 8 7.5 7 1346 412 male 25 72 6.5 7.5 8 7.5 7.5 7.5 831 491 TRUE
female 25 68 5.5 8 7.5 7.5 8 7 284 229 male 26 69 5.5 7.5 7 8 7.5 7 427 230 TRUE
female 23 67 6.5 7.5 7.5 8 7 7 1418 230 male 26 74 6 7.5 6.5 7 7.5 7.5 2137 205 FALSE
female 29 69 5.5 7.5 7.5 7.5 8.5 7.5 705 135 male 30 71 6 8 7.5 8 7.5 7.5 454 263 TRUE
female 28 67 7 8 6.5 7.5 7.5 7.5 1881 230 male 34 75 6 7.5 7.5 7.5 8 7.5 71 15 FALSE
female 28 66 5.5 7.5 7.5 8 7.5 7.5 785 407 male 30 74 7.5 8.5 7.5 8.5 8.5 7.5 1829 439 FALSE
female 24 69 5 7 7.5 7.5 7.5 7.5 633

Hi there, There's no downside to opening connections on initialization or after forking, even if you don't use the connections often. Aside from reducing the chance for connection errors during the request loop, you'll also speed up your request loop by not spending time establishing new connections. I definitely recommend making this change if you can. For Resque, there's unfortunately no way around the fact that every job is run inside a forked process and can't inherit or share any connections. You'll have to open a new Redis connection for every Resque job that uses Redis. The best way to do this inside the "after_fork" Resque hook. If you use ActiveRecord, it's likely that you're already establishing a new connection inside this hook. Here's what our after_fork hook looks like, in our config/initializers/resque.rb file:

Resque.after_fork do
  tries = 0
  begin
    $redis.client.reconnect
    ActiveRecord::Base.establish_connection
  rescue
// CHEFS!!!
import Darwin
var peeps = [
"Tom",
"Chris",
"Connie",
"Rob",
"Yeti",
]