Skip to content

Instantly share code, notes, and snippets.

@tannerwelsh
tannerwelsh / random_word.sh
Created July 10, 2017 18:44
Get a random word of length n
#!/usr/bin/env sh
word_length=$1
all_words=/usr/share/dict/words
# use outer wrapping parens to convert to an array
words=( $(grep -E "^.{$word_length}$" $all_words) )
num_words=${#words[@]} # get length of words array
index=$(($RANDOM % $num_words)) # make a random index
@tannerwelsh
tannerwelsh / commitment.js
Created July 7, 2017 16:44
Reverse-engineering Promise (loosely)
// This is a very bare-bones (and incomplete) implementation of the idea
// behind Promise, using a new function called Commitment:
const Commitment = function(func) {
this.state = 'PENDING' // will be changed to 'FULFILLED' or 'REJECTED'
const resolve = (result) => {
this.state = 'FULFILLED'
this.resolver && this.resolver(result)
}
@peterc
peterc / methods_returning.rb
Last active October 29, 2023 03:10
Object#methods_returning - to work out which method on an object returns what we want
require 'stringio'
require 'timeout'
class Object
def methods_returning(expected, *args, &blk)
old_stdout = $>
$> = StringIO.new
methods.select do |meth|
Timeout::timeout(1) { dup.public_send(meth, *args, &blk) == expected rescue false } rescue false
@jessieay
jessieay / vim
Created October 4, 2012 18:48
VIM vommands
hjkl - move around
:#,#d - deletes between two lines, eg: :10,12d deletes lines 10 through 12
x - delete character
o - insert on next line
O - insert on preceding line
a - insert after cursor
i - go into insert mode
w - move to beginning of next word (also W for whitespace delimited word)
e - move to end of word (also E for whitespace delimited words)
b - move backward to start of word (also B)
@Pcushing
Pcushing / google_drive_parsing.rb
Created June 21, 2012 16:26
Log into google docs and parse the data, work in progress
require 'rubygems'
require 'google_drive'
# Go get your consumer key, client_secret, and client_id for Google Drive here https://code.google.com/apis/console/
consumer_key = 'INSERT YOUR CONSUMER_KEY HERE'
client_secret = 'INSERT YOUR CLIENT_SECRET HERE'
client_id = 'INSERT YOUR CLIENT_ID HERE'
client = OAuth2::Client.new(
client_id, client_secret,