Skip to content

Instantly share code, notes, and snippets.

@aprescott
aprescott / random_stream.rb
Created August 28, 2011 17:07
Simple Enumerable extension to randomly stream elements.
module Enumerable
def random_stream(&block)
enumerator = Enumerator.new do |y|
loop do
y << sample
end
end
block ? enumerator.each(&block) : enumerator
end
@aprescott
aprescott / linked_list_reversal.rb
Created September 28, 2011 19:58
Linked list reversal
class Node
attr_accessor :value, :next_element
def initialize(value, next_element = nil)
@value = value
@next_element = next_element
end
def to_s
return value.to_s if @next_element.nil?
@aprescott
aprescott / object_monitor.rb
Created December 1, 2011 09:24
Monitor an object with delegation.
# BasicObject from
# simonecarletti.com/blog/2010/05/understanding-ruby-and-rails-proxy-patter-delegation-and-basicobject/
class BasicObject
instance_methods.each do |m|
undef_method(m) if m.to_s !~ /^__|^nil\?$|^send$|^object_id$/
end
end
class ObjectMonitor < BasicObject
attr_reader :object
@aprescott
aprescott / liquid_ordinals.liquid
Last active December 13, 2015 20:08
Ordinals in straight Liquid templates.
{% assign d = time | date: "%d" | plus:0 %}
{% assign d_mod = d | modulo:10 %}
{% assign ordinals = "th,st,nd,rd,th,th,th,th,th,th" | split:"," %}
{% if d > 10 and d < 14 %}
{{ d }}th
{% else %}
{{ ordinals[d_mod] }}
{% endif %}
@aprescott
aprescott / gist:4982269
Last active December 13, 2015 22:08
Find your longest man pages.
# Finds your longest man pages by counting `man n foo | wc -l`
# for all man pages available.
man -k . |
sed -e 's/(//' -e 's/)//' |
awk '{ print $2, $1 }' |
xargs -n 1 -I {} bash -c "echo -n -e "{}"'\t'; man "{}" | wc -l" 2>/dev/null |
awk -F$'\t' 'BEGIN { max = 0 } { if ($2 > max) { max = $2; print "max found: " max " (" $1 ")" } }'
@aprescott
aprescott / gist:4996185
Last active December 14, 2015 00:08
Show all lines added to `some-dir/` in the last week (on all branches), restricted to: only newly added files, those by author matching "adam".
#
# Show all lines added to `some-dir/` in the last week (on all branches), restricted to:
#
# * only newly added files,
# * those by author matching "adam".
#
git log --author=adam --since={1.week.ago} --all --diff-filter=A -p -- some-dir/ |
# lines added
grep -E '^\+' |
@aprescott
aprescott / letterboxd-years.sh
Last active December 14, 2015 04:09
Number of films logged in Letterboxd.
#!/bin/bash
USERNAME=your-username
# -----
film_for_year() {
output=$(curl -s "http://letterboxd.com/$1/films/diary/year/$2/" | grep -Eo '([0-9]+)&nbsp;films' | grep -Eo '[0-9]+')
if [ -z "$output" ]; then
@aprescott
aprescott / gist:5121481
Created March 8, 2013 23:58
Permissions and umask
def permissions_string(integer)
triplet_to_string = lambda { |x, s| x.tr("01", "-#{s}") }
integer.to_s(2).rjust(9, "0").chars.each_slice(3).map do |r, w, x|
[
triplet_to_string[r, "r"],
triplet_to_string[w, "w"],
triplet_to_string[x, "x"]
].join("")
end.join("")
@aprescott
aprescott / feed.xml
Last active December 14, 2015 17:49
Sample feed.xml Liquid template for use with Serif 0.3.2. Post unique IDs are independent of URL. Note the caveats!
layout: none
<?xml version="1.0" encoding="utf-8"?>
{% assign feed_url = "http://example.com/feed" %}
{% assign feed_title = "Example Feed" %}
{% assign feed_alternate = "http://example.com/blog" %}
{% assign feed_global_unique_id = "tag:example.com,2013:TOTALLY-RANDOM-STRING-OF-CHARACTERS-GOES-HERE" %}
{% assign feed_author_name = "J. Smith" %}
@aprescott
aprescott / gist:5184336
Created March 18, 2013 00:49
Take a selection in the page and turn it into a blockquote
//
// Take a user's selected content in the page, as HTML, and
// place it inside a blockquote.
//
// Allows the possibility of automatically converting it
// to Markdown with toMarkdown.
//
// the selection in the document -- Selection object
var sel = document.getSelection();