Skip to content

Instantly share code, notes, and snippets.

@tuzz
tuzz / irb.rb
Created March 15, 2012 22:51
A simple demo of my scene gem
# gem install scene
require 'rubygems'
require 'scene'
class MyScene < Scene
def display
@count ||= 0
@count += 1
@count %= 200
@tuzz
tuzz / rubiks.rb
Created March 15, 2012 23:32
A potential project
require 'rubiks'
front = [:green, :green, :white,
:yellow, :green, :green,
:green, :red, :red]
back = [:green, :blue, :yellow,
:green, :blue, :orange,
:yellow, :orange, :white]
@tuzz
tuzz / snippet.bash
Created March 18, 2012 01:25
mp4 conversion function
# Convert videos to mp4 for iTunes/iOS. Use cores * 1.5 as the number of threads.
# Works for most video formats out of the box. ffmpeg must be installed first.
mp4() {
ffmpeg -i "$1" -threads 6 -sameq -acodec libfaac -ar 48000 -ab 160k -ac 2 "$1".mp4
}
@tuzz
tuzz / qtn_patch.rb
Created April 14, 2012 21:06
A useful patch if you're subclassing AR 3.1.0-3.2.2
# Fixes dynamic find by method attempting to use the connection
# on ActiveRecord::Base in activerecord versions 3.1.0 to 3.2.2
# when it should be respecting the connection on its subclass
class QuoteTableNamePatch
def self.quote_table_name(name)
name
end
end
module ActiveRecord
@tuzz
tuzz / without_nil.rb
Created April 15, 2012 11:36
Disallow nil from hash and array
# hash = HashWithoutNilValues.new
# hash[:a] = nil
# hash # => {}
#
# hash[:a] = {}
# hash[:a].class # => HashWithoutNilValues
#
# hash[:a] = []
# hash[:a].class # => ArrayWithoutNilValues
#
@tuzz
tuzz / signature.rb
Created April 24, 2012 22:26
Generate an Unboxed email signature
# Run this: ruby <(curl --silent https://raw.github.com/gist/2484316/signature.rb)
require 'erb'
def format_phone_number(string)
string.gsub!(/^0/, '+44')
string.gsub!(/(...)(..)(....)(....)/, '\1 \2 \3 \4')
end
def blank?(string)
@tuzz
tuzz / config.ru
Created May 5, 2012 18:34
Playing with rack
class Rack::Reverse
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
[status, headers, [body.join.reverse]]
end
@tuzz
tuzz / bootstrap.js
Created May 17, 2012 11:38
Bootstrap a JS file into a page via a bookmark
javascript:void((function(){var%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('src','[[[[[URL HERE]]]]]');document.body.appendChild(e)})())
@tuzz
tuzz / dependencies.rb
Created May 26, 2012 10:32
Speedy gem dependency fetching
# gem install gem-dependent
require 'rubygems/dependent'
def gem_dependencies(limit = nil)
specs_and_sources = Gem::Dependent.all_specs_and_sources(:all_versions => true)
specs_and_sources = specs_and_sources.take(limit) if limit
Gem::Dependent.fetch_all_dependencies(specs_and_sources)
end
puts gem_dependencies(100).inspect
@tuzz
tuzz / sample.rb
Created May 31, 2012 10:59
Sample pre 1.9
unless RUBY_VERSION =~ /1.9/
class Array
def sample
self[rand(self.size)]
end
end
end