Skip to content

Instantly share code, notes, and snippets.

View nathanl's full-sized avatar

Nathan Long nathanl

View GitHub Profile
@nathanl
nathanl / safelog.js
Created December 10, 2010 20:18
A simple wrapper for console.log that doesn't break anything if the user has no console to call
function safelog(message) {
try{ console.log(message); } catch(e){ /*do nothing*/ }
}
#!/bin/sh
set -e
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deploy/taxscribe/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="/usr/local/bin/unicorn --config-file $APP_ROOT/config/unicorn.server.rb --env production --daemonize $APP_ROOT/config.ru"
action="$1"
@nathanl
nathanl / safeLogger
Created August 19, 2011 19:19
Javascript console logging that won't bite you if it goes into production
// Simple wrapper for console.log()
//
// Does nothing unless it's turned on AND the user has a console open
//
// Usage
// - Turn on with `safeLogger.active = true;`
// - Log messages like `safeLogger.log('someString',someObject);`
safeLogger = {
@nathanl
nathanl / git_tag_cleanup.rb
Created January 20, 2012 16:10
Clean up tags in a git repository
# Script to delete all but a specified list of tags
# from a git repo, both locally and remotely
# Run like `mode=test ruby git_tag_cleanup` to test;
# use 'execute' mode to actually delete the tags
mode = ENV['mode'] || 'test'
tags_to_keep = %w[v2.0.0 v2.0.1]
tags_to_delete = `git tag`.split("\n") - tags_to_keep
@nathanl
nathanl / pry_and_debugger_cheat_sheet.md
Created November 15, 2012 14:59
Pry and debugger cheat sheet

Ruby Debugging

Pry

ruby -rpry some_script.rb - the r means require. If there's a binding.pry there, you'll be on it.

Pry gives you a graphical look at your program in progress, lets you cd among objects, ls to see available variables, etc. You can't step, though; just explore a snapshot at that moment.

Debugger

Must install debugger gem for your version of Ruby (1.9 is 'gem install debugger'). Doesn't play well with Spork.

@nathanl
nathanl / transpose_lyrics.rb
Created December 5, 2012 10:11
Rearrange song lyrics so it's easier to put guitar chords over them
#!/usr/bin/env ruby
# Rearrange song lyrics so it's easier to put guitar chords over them: put the
# first lines of each verse, which share the same chords, into the first
# paragraph. Second lines go into the second paragraph, etc.
#
# Meant to be used with Unix piping: `cat somefile.txt | ./this_script > output.txt
lyrics = STDIN.read
paragraphs = lyrics.split("\n\n")
lines = paragraphs.map { |paragraph| paragraph.split("\n") }
@nathanl
nathanl / activerecord_scope_conditions.rb
Created June 11, 2013 14:45
Using joins and conditions in ActiveRecord scopes. (This was hard for me to find documentation on.)
class Person < ActiveRecord::Base
# This manual SQL query...
scope :allowed_to_eat_cheese, joins(
<<-SQL
INNER JOIN cities ON people.city_id = cities.id
INNER JOIN states ON cities.state_id = states.id
SQL
).where('states.allows_cheese_consumption' = 'Yeppers')
@nathanl
nathanl / safe_console.js
Last active December 21, 2015 21:29
Safer console.log
// A safer console object (https://gist.github.com/nathanl/6368185)
// - Ensures `console.log` doesn't cause errors in browsers with no console
// - Lets you enable/disable console logging (using console.enable = true/false)
// - Supports all console methods documented here: https://developer.mozilla.org/en-US/docs/Web/API/console
//
// Less fancy but lighter weight than this: http://benalman.com/projects/javascript-debug-console-log/
safe_console = {
enabled: false,
original_console: (function(){
// If the browser has no usable one, define a no-op
@nathanl
nathanl / ruby_log_formatting.rb
Created February 11, 2014 14:49
Nice Ruby log formatting
require 'logger'
l = Logger.new(STDOUT)
l.formatter = proc { |severity, datetime, progname, msg|
dt = datetime.strftime('%Y-%b-%d@%H:%M:%S:%z')
"#{[severity,dt,progname,msg].join(' ').squeeze(' ')}\n"
}
l.info "woot" #=> INFO 2014-Feb-11@09:48:32:-0500 woot
@nathanl
nathanl / p.rb
Created August 8, 2014 19:10
Ruby `p` with location
def p(*args)
location = caller_locations(1,1)[0]
location_string = "#{location.path.split('/').last}:#{location.lineno}(#{location.label})"
super([*args, location_string])
end