Skip to content

Instantly share code, notes, and snippets.

# settings
@bucket_count = 1000 # Must be integer > 0
@min_value = 10 ** -4 # Must be > 0.0
@max_value = 10 ** +2 # Must be > @min_value
# precalculate some stuff
@min_value_log = Math.log(@min_value)
@max_value_log = Math.log(@max_value)
@log_delta = @max_value_log - @min_value_log
@bucket_size = @log_delta / @bucket_count.to_f
@wvanbergen
wvanbergen / bmp_import.rb
Created August 18, 2010 22:59
Create PNG from 24 bit BMP file
require 'rubygems'
require 'chunky_png'
# get the data
data = StringIO.new(File.read('filename.bmp'))
bmp_file_header = data.read(14).unpack('a2VxxV')
bmp_info_header = data.read(40).unpack('V3v2V6')
raise "Not a BMP file" unless bmp_file_header[0] == "BM"
@wvanbergen
wvanbergen / 1.8.7 (ruby-1.8.7-p299): ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-darwin10.4.0]
Created July 28, 2010 17:07
Benchmark results of ChunkyPNG using different interpreters
---------------------------------------------
ChunkyPNG (0.8.0) decoding benchmark (n=50)
---------------------------------------------
Rehearsal ---------------------------------------------------------
From PNG stream 16.050000 0.110000 16.160000 ( 16.747523)
From RGBA pixelstream 0.090000 0.030000 0.120000 ( 0.122060)
From RGB pixelstream 0.860000 0.030000 0.890000 ( 0.901062)
----------------------------------------------- total: 17.170000sec
@wvanbergen
wvanbergen / gist:204134
Created October 7, 2009 15:30
Audio-enabled, profanity-enabled unit testing
module Swearing
SWEAR_WORDS = ['dammit', 'fuck', 'son of a bitch', 'motherfucker', 'oh no', 'prick']
SWEAR_VOICE = 'Alex'
def self.swear!
`say -v #{SWEAR_VOICE} #{SWEAR_WORDS[rand(SWEAR_WORDS.length)]} &`
end
end
class Test::Unit::Error
@wvanbergen
wvanbergen / ParseTree.eval_static_tree.rb
Created October 3, 2009 11:50
A method to evaluate static parts of a ParseTree syntax tree.
class ParseTree
# Evaluates a static part of the tree that ParseTree.translate returns.
# You can pas a block that will be used to get a value that is not static.
# A simple example:
#
# >> code = '{ :array => ["str", 123, 4.5], :dynamic => method_call }'
# >> tree = ParseTree.translate(code)
# => [:hash, [:lit, :array], [:array, [:str, "str"], [:lit, 123], [:lit, 4.5]],
# [:lit, :dynamic], [:vcall, :method_call]]
@wvanbergen
wvanbergen / gist:5017
Created August 12, 2008 07:44 — forked from dbussink/gist:4461
Add the current git branch in pink to your bash prompt.
# colors for ls, etc.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \[\1\]/'
}
if [[ ${EUID} == 0 ]] ; then
PS1='\[\033[01;31m\]\u@\h \[\033[01;34m\]\w\[\033[01;35m\]$(parse_git_branch)\[\033[01;34m\] \$ \[\033[00m\]'
else
PS1='\[\033[01;37m\]\w\[\033[00;35m\]$(parse_git_branch)\[\033[00m\] \$ '
fi
____ ____ ____ _______
| | | _/
| | | \o/ _/
| | | | _/
| | ==> | /\/
| o | | _/
| |- | | _/
|_/\_| |____/
@wvanbergen
wvanbergen / Makefile
Last active August 29, 2015 14:19
Makefile to install and run the confluent inc platform for CI purposes.
.PHONY: confluent/kafka/* confluent/zookeeper/* confluent/registry/* confluent/start confluent/stop
# Confluent platform tasks
confluent/start: confluent/rest/start
confluent/stop: confluent/rest/stop confluent/registry/stop confluent/kafka/stop confluent/zookeeper/stop
# Download & extract tasks
@wvanbergen
wvanbergen / bitwise.rb
Last active August 29, 2015 13:56
0xabc to 0xaabbcc using bitwise operators
# There must be a nicer way than this
((x & 0xf00) << 12 | (x & 0xf00) << 8) |
((x & 0x0f0) << 8 | (x & 0x0f0) << 4) |
((x & 0x00f) << 4 | (x & 0x00f))