Skip to content

Instantly share code, notes, and snippets.

@heisters
heisters / three.orbitcontrols.js
Last active August 29, 2015 14:06
Orbit Controls for THREE.js
/**
* @author qiao / https://github.com/qiao
* @author mrdoob / http://mrdoob.com
* @author alteredq / http://alteredqualia.com/
* @author WestLangley / https://github.com/WestLangley
*/
THREE.OrbitControls = function ( object, domElement ) {
THREE.EventDispatcher.prototype.apply( this );
@heisters
heisters / gifify.sh
Created March 26, 2015 23:31
Convert videos to gifs
#!/bin/bash
in="$1"
out="$2"
fps=6
delay=$(printf "%0.f" $(echo "scale=2;100 / $fps" | bc))
mkdir gif
cd gif
ffmpeg -i $in -r $fps frame%05d.png
@heisters
heisters / pbhi.sh
Created May 26, 2015 18:25
Add syntax highlighting to the current pastebuffer
#!/bin/bash
# brew install highlight
pbpaste | highlight --syntax bash -k 'Andale Mono' -s molokai -O rtf | pbcopy
@heisters
heisters / script_spec.rb
Created April 23, 2009 21:19
rspec I/O for a ruby script
require 'open3'
describe 'my_script' do
def run *args
options = args.extract_options!
args.unshift './my_script'
Open3.popen3(args.join(' ')) do |i, o, e|
i.write options[:stdin]
i.close
@heisters
heisters / object_try.rb
Created April 24, 2009 17:17
a more succinct implementation of Object#try
# Makes it easy to try more than one method on Object and just return nil if
# the Object doesn't respond to any of the methods. So, rather than:
#
# obj.try(:foo).try(:bar)
#
# you can just
#
# obj.try(:foo, :bar)
#
# Based on Object#try from ActiveSupport.
@heisters
heisters / enumerable_try_keys.rb
Created April 24, 2009 20:37
access elements deep in a nested enumerable safely
# Makes it easy to access elements deep within a nested Enumerable. Rather than
# having to do
#
# enum[:foo] && enum[:foo][:bar]
#
# you can just do
#
# enum.try_keys :foo, :bar
module Enumerable
def try_keys *keys
@heisters
heisters / render_status_codes.rb
Created May 5, 2009 17:02
automatically define Rails render_* methods for all possible HTTP responses
class ApplicationController < ActionController::Base
protected
# Define render_xxx methods for all known status codes.
ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.each do |symbol, code|
define_method "render_#{symbol}" do |*args|
respond_to do |format|
format.html{render_error_html_file(code, (args.first || {}))}
format.all{head symbol}
end
@heisters
heisters / Rakefile.rb
Created June 2, 2009 00:41
a rake task to pull gists from github.com as html
# A rake task to pull all your gists from github.com as html.
#
# This has advantages over the script embed:
# * the script uses document.write, which is not compatible with XHTML Strict 1.0
# * document.write also can't be used after the document body has loaded (ie. no AJAX)
# * the gists get indexed by search engines this way
desc "Pull all gists from github and cache them locally"
task :gists do
uri = URI.parse 'http://gist.github.com/api/v1/yaml/gists/heisters'
html = YAML.load(Net::HTTP.get(uri))['gists'].inject('') do |h, gist|
@heisters
heisters / tshark_http.sh
Created June 15, 2009 00:34
Make TShark dump and dissect an HTTP connection
# Make TShark dump and dissect an HTTP connection
sudo tshark -i3 -V -T text -i en1 -f "tcp port 80 and host <host ip>" -d "tcp.port==80,http" -R http.request
@heisters
heisters / temporary_dsl.rb
Created July 24, 2009 20:58
A Ruby 1.9 DSL strategy
# An unintrusive, temporary DSL strategy.
#
# Why discusses Ruby DSLs and instance_eval vs. block arguments:
# http://hackety.org/2008/10/06/mixingOurWayOutOfInstanceEval.html
#
# This takes advantage of some Ruby 1.9 features to implement a DSL mixin that
# is temporary and doesn't override locally defined methods.
class DslInstance
attr_accessor :script