Skip to content

Instantly share code, notes, and snippets.

@robertjwhitney
robertjwhitney / download.rb
Created February 3, 2011 17:17
simple script to download an array of images over http
require 'net/http'
require 'yaml'
files = YAML.load_file("image_list.yml")
i = 1
files.each do |file|
# use root url without protocol
response = Net::HTTP.start("farm5.static.flickr.com") do |http|
http.get("#{file}")
end
puts "#{response}"
@robertjwhitney
robertjwhitney / film.rb
Created April 22, 2012 23:45
Conditionally display an image instead of the file input in an ActiveAdmin has_many association.
# active_admin and formtastic seem kinda picky about displaying both the
# image and the file input together here... in fact, it seems like this
# is the ONLY way to do this? Adding anything else after the image_tag squashes it.
ActiveAdmin.register Film do
form do |f|
f.inputs "Film" do
f.input :title
end
f.has_many :stills do |film_still_form|
@robertjwhitney
robertjwhitney / spellcheck.rb
Last active November 2, 2016 09:30
Use Google's undocumented spellcheck API to make spelling correction suggestions.
# Unfortunately, Google doesn't return the actual string it is suggesting
# replacements for, so this got a little complicated. Fork and improve!
require 'httparty'
require 'nokogiri'
# > Spellcheck.correct("Ths is a tst")
# => "This is a test"
# > Spellcheck.correct("My nme is Robrt Whtney")
# => "My name is Robert Whitney"
@robertjwhitney
robertjwhitney / activeadmin_sortable.js.coffee
Created July 3, 2012 03:37
Simple drag/drop reordering of records in an ActiveAdmin view table
# http://stackoverflow.com/a/8936202
#
# ActiveAdmin already includes the necessary jquery in active_admin/base,
# so just add this to javascripts/active_admin.js after //= require active_admin/base
#
#
# Serialize and Sort
#
# model_name - you guessed it, the name of the model we are calling sort on.
# This is the actual variable name, no need to change it.
# multi-class selection
<div class="a b c">
</div>
.a.b { }
# parent - child selection
<div class="a">
class Object
# Return a list of methods defined locally for a particular object. Useful
# for seeing what it does whilst losing all the guff that's implemented
# by its parents (eg Object).
def local_methods(obj = self)
(obj.methods - obj.class.superclass.instance_methods).sort
end
end
$ valgrind --version
valgrind-3.8.1
$ valgrind ./ex6
==56827== Memcheck, a memory error detector
==56827== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==56827== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==56827== Command: ./ex6
==56827==
=begin
## Description
A tool for Octopress to generate IDs in header tags and index list aside. It helps you easily build a tutorial page with many chapters.
## Features
* Support all languages.
## Syntax
@robertjwhitney
robertjwhitney / tower_of_hanoi.rb
Last active December 13, 2015 18:18
A lesson in recursion
# LETS GET READY TO RECURSE!!!!!!
#
# Tower of Hanoi
#
# You have 4 discs which vary in size from small to large
# There are 3 pegs, A, B, C, respectively
#
# The discs are all currently on peg A, with the largest disc on the bottom,
# and the smallest disc on the top.
#
# Generate the sequence of numbers below.
# 0 1 2 3 4 5 6 7 8 9
# 1 2 3 4 5 6 7 8 9 0
# 2 3 4 5 6 7 8 9 0 1
# 3 4 5 6 7 8 9 0 1 2
# 4 5 6 7 8 9 0 1 2 3
# 5 6 7 8 9 0 1 2 3 4
# 6 7 8 9 0 1 2 3 4 5
# 7 8 9 0 1 2 3 4 5 6