Skip to content

Instantly share code, notes, and snippets.

@samstokes
samstokes / methods_returning.rb
Created October 21, 2011 22:44
Stick this in your .irbrc and smoke it
def methods_returning(answer, *args)
# probably want to filter out some methods
# e.g. ones that mutate their receiver...
blacklist = %w()
(methods - blacklist).select do |method|
guinea_pig = begin
clone
rescue TypeError => e
raise unless e.message =~ /can't clone/
@samstokes
samstokes / dg_loop_syntaxen.rb
Created November 7, 2011 23:01
DG loop combinator thinking
# Hmmm
DG::loop_until { url.present? && chunks_done < 25 }.body do
make_http_request(stuff, things, other_things).safe_callback do |blah|
blah
end
end.transform
# Yum
DG::loop_until(lambda { url.present? && chunks_done < 25 }) do
make_http_request(stuff, things, other_things).safe_callback do |blah|
@samstokes
samstokes / dg_example_bind.rb
Created November 14, 2011 22:08
DG example: bind: following links on a Google search
EM.run do
fetch('http://google.com/search?q=deferrable_gratification').bind! do |doc|
fetch((doc / 'ol' / 'li' / 'a')[0][:href])
end.bind! do |doc|
fetch((doc / '#repository_homepage').at(:a)[:href])
end.callback do |doc|
puts doc.at(:title).inner_text
# now the previous 'doc's aren't in scope, so I can't accidentally
# refer to them
end.errback do |error|
@samstokes
samstokes / test.rb
Created April 18, 2012 01:22
Test public Ruby gist
# This is a large amount of Ruby code
# No, really, it is
# Can't you tell?
def foo(bar)
baz(bar)
end
# That was a large amount of Ruby code
# Wiffle bamble boo
@samstokes
samstokes / stash
Created November 6, 2012 22:08
example git log with a stash
sstokes@sstokes-mba-ubuntu:gittest$ git log --all --decorate --graph --oneline
* 15e2132 (refs/stash) WIP on master: 2e83ed8 initial commit
|\
| * 61442a1 index on master: 2e83ed8 initial commit
|/
* 2e83ed8 (HEAD, master) initial commit
@samstokes
samstokes / todo
Last active January 3, 2025 01:29
A todo management system in a gist
#!/usr/bin/env bash
set -e
if [[ $# > 0 ]]; then
case "$1" in
-h | -\? | --help )
{
echo "Add a todo:"
echo " todo Reformulate the widget plans."

Keybase proof

I hereby claim:

  • I am samstokes on github.
  • I am samstokes (https://keybase.io/samstokes) on keybase.
  • I have a public key ASBjVCcl1P5w4m-tmbthV5BAb3AOcY4g2Uxu3v7G6e8kXAo

To claim this, I am signing this object:

@samstokes
samstokes / sqlalchemy_logging.py
Created May 6, 2016 20:53
Turning on SQL logging in SQLAlchemy
import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

Mocha Bourbon

Two-stage infusion.

Stage 1: coffee

  • ¼cup coffee beans
  • 16oz Bourbon (works with rye too)

Infuse for 24 hours, then strain into a second jar.

@samstokes
samstokes / README.md
Created January 7, 2023 01:49
Handling Disqus export in Jekyll

Script to parse Disqus Comment Export XML as documented at https://help.disqus.com/en/articles/1717164-comments-export and translate it into a JSON format that is easier to handle (for example, using Jekyll's built-in support for JSON files in _data).

The imagined use case is if you have decided to remove Disqus comments from your blog (or maybe disable commenting entirely), but have existing comments on previous posts that you want to keep. You can export your comments from Disqus, and statically render the historical comments, without needing any Disqus Javascript or cookies.

The Disqus XML exports all categories, threads and posts into flat lists, with id references for parent relationships. That makes it complicated to do things like "look up all comments on this blog post", or see which comments were in reply to which,