Skip to content

Instantly share code, notes, and snippets.

@nahurst
nahurst / gist:d2f6f9b18bc8638a3e25
Created October 23, 2014 21:16
Get an angular service from the console
service = angular.element(document).injector().get('cubeService')
service.track('plb_test', {'hi' : 'nathan'})
Verifying that +nahurst is my openname (Bitcoin username). https://onename.com/nahurst
@nahurst
nahurst / history.sh
Created June 10, 2015 21:28
Teach us something technical: command line
# set these in your .bash_profile, .bashrc, .zshrc first
HISTFILESIZE=100000 # how many lines to keep total
HISTSIZE=100000 # how many lines to keep for the current session
# oh-my-zsh will default to a reasonable 10000, but most bash defaults to a few hundred
# oh-my-zsh by default appends all commands to history after they are run instead of on session end
# search the history with control-r
# search for commands starting with the one you typed with alt-p
# if you don't want something to stay in your history, prefix the command with a space
@nahurst
nahurst / file_name_expansion.sh
Created January 17, 2011 19:32
Bash file name expansion
# make a quick backup
cp file{,.bak}
# access files with the same name in different directories
git add app/views/{users,roles}/{index,new}.html
@nahurst
nahurst / bit_hacks.c
Created January 17, 2011 19:34
Bit Hacks
v &= v - 1; // clear the least significant bit set
((x ^ y) < 0); // true iff x and y have opposite signs
//more here http://graphics.stanford.edu/~seander/bithacks.html
@nahurst
nahurst / and_or.rb
Created January 27, 2011 19:26
Ruby and/or vs &&/||
# "and" and && are not synonymous in Ruby
# "and" and "or" have a much lower precedence
# "and" and "or" are more like control flow operators than boolean operators
do_this() or raise "Failure"
assign_me = 10 and assign_me / 2 # (assign_me = 10) and assign_me / 2 ... => 5
#vs
assign_me = 10 && assign_me / 2 # assign_me = (10 && assign_me) / 2 ... => undefined
@nahurst
nahurst / repo_browsers.sh
Created January 27, 2011 19:37
Repository Viewer
# Browse your local git repository
# If you prefer not to use webrick, you could install lighttpd and then just run git instaweb.
git instaweb --httpd webrick # view at http://localhost:1234
# Browse your local Mercurial repository
hg serve # view at http://localhost:8000
@nahurst
nahurst / lcs.js
Created February 21, 2011 21:27
Longest Common Subsequence
/**
* Find a longest common subsenquence.
*
* Note: this is not necessarily the only possible longest common subsequence though!
*/
function lcs(listX, listY) {
return lcsBackTrack(
lcsLengths(listX, listY),
listX, listY,
listX.length, listY.length);
@nahurst
nahurst / rails_time.rb
Created April 24, 2011 17:17
Rails Time
# ActiveSupport::TimeWithZone is a rails construct. Always use it for querying and creating.
# DateTime is an old class and is very Ruby specific (not Rails). Don't use it.
# If you put .utc on the end of something other than a DateTime, it's converted to a Time.
# <class> <to_s>
Time.now # Time Sun Apr 24 13:00:00 -0400 2011
Time.now.utc # Time Sun Apr 24 17:00:00 UTC 2011
Time.now.in_time_zone('UTC') # ActiveSupport::TimeWithZone 2011-04-24 17:00:00 UTC
Time.zone.now # ActiveSupport::TimeWithZone 2011-04-24 17:00:00 UTC
Time.zone.now.utc # Time Sun Apr 24 17:00:00 UTC 2011
@nahurst
nahurst / mirror.sh
Created May 17, 2011 22:18
Mirror a site
wget -w 1 -r -k -K -H -N -l 2 http://example.com # pause 1 second between requests, go 2 levels deep