Skip to content

Instantly share code, notes, and snippets.

@bmarini
bmarini / async.coffee
Created November 16, 2011 17:30
async callbacks in a loop
app.get '/riders', (req, res) ->
redis.smembers 'riders', (err, replies) ->
results = []
build_json = (rider) ->
redis.hgetall rider, (err, reply) ->
results.push reply
res.json(results, 200) if results.length == replies.length
build_json rider for rider in replies
@bmarini
bmarini / onscreen.sh
Created October 14, 2011 17:20
A little time saver for cd'ing into my most common directory (current project I am working on)
onscreen()
{
if [ -z $1 ]; then
if [ -f $HOME/.onscreen ]; then
cd `cat $HOME/.onscreen`
else
echo "You must set an onscreen destination"
echo "cd into a directory and run 'onscreen set'"
fi
fi
@bmarini
bmarini / model-view-ideas.rb
Created October 10, 2011 00:17
Tradeoffs
class User < Struct.new(:name, :phone, :email)
end
# ---------------------------------------------------------------------------
# Extend on demand
# + no dependency on User class, could be reusable
# + composable
# - cannot unextend user object once extended, but this object could be short
# lived
# ---------------------------------------------------------------------------
@bmarini
bmarini / progress_bar.rb
Created September 14, 2011 21:55
CLI progress bar in ruby
class ProgressBar
def initialize(units=60)
@units = units.to_f
end
def print(completed, total)
norm = 1.0 / (total / @units)
progress = (completed * norm).ceil
pending = @units - progress
Kernel.print "[#{'=' * progress }#{' ' * ( pending )}] #{percentage(completed, total)}%\r"
@bmarini
bmarini / local-replication.sh
Created August 25, 2011 01:18
Local Replica Set
mkdir ~/tmp_mongo_data1
mkdir ~/tmp_mongo_data2
mkdir ~/tmp_mongo_data3
# In separate terminals
mongod --dbpath ~/tmp_mongo_data1 --port 5000 --replSet ben
mongod --dbpath ~/tmp_mongo_data2 --port 5001 --replSet ben
mongod --dbpath ~/tmp_mongo_data3 --port 5002 --replSet ben
# Then setup replication in a 4th terminal
@bmarini
bmarini / default.vcl.pl
Created June 30, 2011 18:01
A good varnish config for a Rails app
# https://www.varnish-cache.org/docs/2.1/tutorial/vcl.html
# https://www.varnish-cache.org/trac/wiki/VCLExamples
# Summary
# 1. Varnish will poll the backend at /health_check to make sure it is
# healthy. If the backend goes down, varnish will server stale content
# from the cache for up to 1 hour.
# 2. Varnish will pass X-Forwarded-For headers through to the backend
# 3. Varnish will remove cookies from urls that match static content file
# extensions (jpg, gif, ...)
@bmarini
bmarini / ruby-prof-scaffold.rb
Created June 14, 2011 18:08
ruby-prof scaffold code
require 'ruby-prof'
# RubyProf.measure_mode = RubyProf::PROCESS_TIME
# RubyProf.measure_mode = RubyProf::WALL_TIME
# RubyProf.measure_mode = RubyProf::CPU_TIME
# RubyProf.measure_mode = RubyProf::ALLOCATIONS
# RubyProf.measure_mode = RubyProf::MEMORY
# RubyProf.measure_mode = RubyProf::GC_RUNS
# RubyProf.measure_mode = RubyProf::GC_TIME
result = RubyProf.profile do
@bmarini
bmarini / Rakefile
Created May 17, 2011 01:39
Ronn manual for your gh-pages branch (taken from ddollar/foreman)
desc 'Build the manual'
task :man do
require 'forker/version'
ENV['RONN_MANUAL'] = "Forker Manual"
ENV['RONN_ORGANIZATION'] = "Forker #{Forker::VERSION}"
sh "ronn -w -s toc -r5 --markdown man/*.ronn"
end
@bmarini
bmarini / bash-one-liners.sh
Created May 5, 2011 22:05
Bash one liners
# Start a job from an SSH session that will continue to run after logging out
nohup ./myprogram > foo.out 2> foo.err < /dev/null &
# Clear memcached
echo flush_all | nc localhost 11211
@bmarini
bmarini / threadpool.rb
Created May 2, 2011 15:30
Playing around with EM
require 'rubygems'
require 'bundler/setup'
require 'eventmachine'
require 'httparty'
# Serial Processing:
# results = %[realtime forker].collect do |path|
# HTTParty.get "http://github.com/api/v2/json/repos/show/#{path}"
# end
#