Skip to content

Instantly share code, notes, and snippets.

View Alex-Poon's full-sized avatar

axp Alex-Poon

  • facebook
View GitHub Profile
@bcalloway
bcalloway / rails-export-csv.rb
Created July 29, 2009 22:08
Use FasterCSV to export an ActiveRecord object in Rails
### /config/environment.rb
config.gem 'fastercsv'
### /config/routes.rb
map.connect '/users/export', :controller => 'users', :action => 'export'
### /app/models/user.rb
# find all students and pass to controller export action for export to csv
def self.find_students
find_by_sql("select students.firstname, students.lastname, students.grade, students.homeroom, students.phone, students.email, students.relationship, users.firstname, users.lastname, accounts.school from students, users, accounts where students.user_id = users.id and accounts.id = users.account_id")
@zhengjia
zhengjia / capybara cheat sheet
Created June 7, 2010 01:35
capybara cheat sheet
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
@ocean90
ocean90 / gist:1544377
Created December 31, 2011 15:43
Compiling Compass.app on Mac OS X
Tutorial for compiling the Compass.app (http://compass.handlino.com/)
=====================================================================
Java installed?
java -version
Load jRuby (for example JRuby 1.6.5.1 Binary .zip)
http://jruby.org/download
Unzip jRuby to `/usr/local/`
@guyboltonking
guyboltonking / compressed_static_assets.rb
Created March 21, 2012 20:37
Slightly hacky rails middleware for serving up precompiled gzipped assets
require 'action_dispatch/middleware/static'
module Middleware
class FileHandler < ActionDispatch::FileHandler
def initialize(root, assets_path, cache_control)
@assets_path = assets_path.chomp('/') + '/'
super(root, cache_control)
end
def match?(path)
@jboner
jboner / latency.txt
Last active June 2, 2024 11:03
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@camertron
camertron / measure.rb
Created June 15, 2012 22:48
Measure the memory taken by a Ruby object (by Robert Klemme)
#!/bin/env ruby
# lazy hack from Robert Klemme
module Memory
# sizes are guessed, I was too lazy to look
# them up and then they are also platform
# dependent
REF_SIZE = 4 # ?
OBJ_OVERHEAD = 4 # ?
@brandonb927
brandonb927 / osx-for-hackers.sh
Last active May 29, 2024 14:52
OSX for Hackers: Yosemite/El Capitan Edition. This script tries not to be *too* opinionated and any major changes to your system require a prompt. You've been warned.
#!/bin/sh
###
# SOME COMMANDS WILL NOT WORK ON macOS (Sierra or newer)
# For Sierra or newer, see https://github.com/mathiasbynens/dotfiles/blob/master/.macos
###
# Alot of these configs have been taken from the various places
# on the web, most from here
# https://github.com/mathiasbynens/dotfiles/blob/5b3c8418ed42d93af2e647dc9d122f25cc034871/.osx
@morphizer
morphizer / process_slow_logs.py
Created October 17, 2012 03:32
Scripts to send mysql slow query logs to anemometer for analysis
#!/usr/bin/env python
# Processes the files in the incoming directory for query analysis in anemometer
# Supply the db user/pass/host
import glob, os, shutil, sys
from subprocess import Popen
db_user = "anemometer"
db_pass = "password"
@subelsky
subelsky / puma_rails_heroku.rb
Created October 31, 2012 13:51
Setting up Puma and Rails on Heroku
# Gemfile
gem "puma"
# Procfile
web: bundle exec puma -p $PORT -e $RACK_ENV -C config/puma.rb
# add to config block config/environments/production.rb
config.threadsafe!
# get rid of NewRelic after_fork code, if you were doing this:
@jokull
jokull / gist:5498543
Created May 1, 2013 21:30
Example of an RQ exception handler that allows three failures before moving to the failed queue
queues = []
def retry_handler(job, exc_type, exc_value, traceback):
# Returning True moves the job to the failed queue (or continue to
# the next handler)
job.meta.setdefault('failures', 1)
job.meta['failures'] += 1
if job.meta['failures'] > 3 or isinstance(exc_type, (LookupError, CorruptImageError)):
job.save()