Skip to content

Instantly share code, notes, and snippets.

@hawx
hawx / a_bench.rb
Created February 10, 2011 17:19
Setting local variables within procs object, with benchmarks
require_relative 'local_in_procs'
f = lambda { message }
a = {:message => "Hello, world!"}
# Test the speed when using normal proc params
f_control = lambda {|a| a[:message] }
def rand_letters(n, l)
r = []
@hawx
hawx / colours.rb
Created December 16, 2010 17:24
Add colour to your output.
# Usage:
#
# puts green("I'm green")
# puts red("I'm red")
# puts bold("I'm bold")
#
def colour(text, code)
"#{code}#{text}\e[0m"
end
# Matches an Array (of trues and falses) to a given Array. Fills itself
# with preference to fill where true first, then any spare items are put
# in place of false, then nils are used for remaining falses.
#
# input = ["a", "b", "c"]
# match = [true, false, false, true]
#
# match.optimise_fill(input)
# #=> ["a", "b", nil, "c"]
#
@hawx
hawx / scoped-monkey-patch.rb
Created July 26, 2010 16:16
This is how I would maybe work around the problem of scoping monkey patches. Which should be coming in Ruby 2.0
module Test
class String < ::String; end
p String.new("Test::String").class
#=> Test::String
p ::String.new("::String").class
#=> String
p "::String".class
#=> String -> Would be better as Test::String
end
# Gets the genres for tracks in a playlist called 'Genres'in iTunes
# and sets them using data from wikipedia.
#
# Uses the album name for the search, but won't try anything fancy
# if it can't find the labum it will just ignore it.
#
# ref: http://www.apeth.com/rbappscript/05propel.html
require 'appscript'
require 'nokogiri'
@hawx
hawx / Rakefile
Created July 4, 2010 15:41
Minify a folder of js with `rake minify`
desc "minify js and put into one file"
task :minify do
require 'jsmin'
out = 'min.js'
glb = '*.js'
res = ''
Dir[glb].each do |path|
res << File.read(path) << "\n"