This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// setInterval implemented using setTimeout | |
function mySetInterval(fn, delay) { | |
setTimeout(function () { | |
fn(); | |
mySetInterval(fn, delay); | |
}, delay) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Cat = (function () { | |
var numberOfCats = 0; | |
var Cat = function (name) { | |
this.name = name; | |
numberOfCats += 1; | |
} | |
Cat.prototype.numberOfCats = function () { | |
return numberOfCats; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'openssl' | |
def gravitar_url(email) | |
base = 'http://www.gravatar.com/avatar/' | |
base + OpenSSL::Digest::MD5.new(email).to_s | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
noremap : ; | |
noremap ; : | |
set number | |
set background=dark | |
set textwidth=80 | |
set paste | |
set foldmethod=indent | |
set nofoldenable "dont fold by default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nums = [1, 2, 3] | |
nums.map { |num| num.even? } # => [false, true, false] | |
nums.map(&:even?) # => [false, true, false] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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! | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
OlderNewer