Skip to content

Instantly share code, notes, and snippets.

View jhamon's full-sized avatar

Jennifer Hamon jhamon

View GitHub Profile
@jhamon
jhamon / class-variable.js
Created April 20, 2014 11:42
Shared mutable state among "instances of a class" in javascript.
// In this gist, I explain how to achieve mutable shared state
// between "instances of a class", i.e. objects with a shared
// prototype. This mimics how "class variables" are used
// in other languages with classical inheritance.
// Not something you want to do all the time, obviously, but it could
// be handy in some situations.
function Cat() {
// increment the class variable each time a constructor is used.
// setInterval implemented using setTimeout
function mySetInterval(fn, delay) {
setTimeout(function () {
fn();
mySetInterval(fn, delay);
}, delay)
}
@jhamon
jhamon / class-variables-through-closure.js
Created April 23, 2014 20:12
Another way to achieve private class variables using closure.
var Cat = (function () {
var numberOfCats = 0;
var Cat = function (name) {
this.name = name;
numberOfCats += 1;
}
Cat.prototype.numberOfCats = function () {
return numberOfCats;
@jhamon
jhamon / gravatar.rb
Created May 2, 2014 10:18
Build the gravitar image avatar url from a user email address.
require 'openssl'
def gravitar_url(email)
base = 'http://www.gravatar.com/avatar/'
base + OpenSSL::Digest::MD5.new(email).to_s
end
@jhamon
jhamon / clever-api.rb
Created June 23, 2014 18:17
Finding the average number of students per section with the Clever API.
require 'net/https'
require 'json'
uri = URI('https://api.clever.com/v1.1/sections?limit=379')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Get.new(uri.request_uri)
@jhamon
jhamon / texcleanup.py
Last active December 16, 2015 13:09
Delete helper files created by running pdflatex. I hate having all that clutter in my directories after I'm done building a document or beamer presentation. Retains pdf, sty, tex, and bib files.
#!/usr/bin/python
import os
# This script deletes the garbage files generated when compiling a
# latex document. Run it from the directory you would like cleaned
# up. Source documents (those ending with *.tex, *.bib, and *.sty)
# and outputs (e.g. postscript and pdf files) are not affected.
filelist = os.listdir(os.getcwd())
noremap : ;
noremap ; :
set number
set background=dark
set textwidth=80
set paste
set foldmethod=indent
set nofoldenable "dont fold by default
nums = [1, 2, 3]
nums.map { |num| num.even? } # => [false, true, false]
nums.map(&:even?) # => [false, true, false]
@jhamon
jhamon / object_ids_and_mutability.rb
Created September 18, 2013 05:09
W1D2 - References, object_id, and mutability
## Be careful when referencing mutable objects
a = {} # => {}
b = a # => {}
a.object_id # => 70132601471180
b.object_id # => 70132601471180
a[:foo] = 'bar' # => "bar"
b # => {:foo=>"bar"} # Woah!
## Mutable objects as hash keys: probably a bad idea.
silly_array = ['fox', 'cow', 'moose']
another_ref = silly_array
# These variable names refer to the same object
silly_array.object_id == another_ref.object_id # => true
# Even though it doesn't seem like a great idea,
# ruby will let us use mutable objects, like