Skip to content

Instantly share code, notes, and snippets.

@whylom
whylom / count.rb
Created October 24, 2014 22:18
Count instances of each member of an array
# given an array
words = %w(duck duck goose cry baby cry)
# generate a hash that counts occurrences of each word, eg:
# {"duck"=>2, "goose"=>1, "cry"=>2, "baby"=>1}
# Method #1
counts = Hash.new(0)
words.each { |word| counts[word] += 1 }
@whylom
whylom / README.md
Created December 4, 2014 23:43
A parser for Heroku log messages

A parser for Heroku log messages

This parser uses the Parslet to define rules for parsing a log message in the simple key=value format used by Heroku, eg:

at=error code=H12 desc="Request timeout" method=GET path="/foo" dyno=web.3 connect=1ms service=30000ms status=503 bytes=0

Given a message in the above format, the parser returns a hash of the key/value pairs:

@whylom
whylom / spider.js
Created December 24, 2014 16:15
dot.js spider checking a site for broken images
function advance() {
var url = document.location.href;
var parts = url.split('/');
var id = parts[parts.length-1];
var nextID = parseInt(id) + 1;
document.location = "https://site.com/resources/" + nextID;
}
var image = $('img.should-be-here');