Skip to content

Instantly share code, notes, and snippets.

% list of {type, flag} tuples
pack(List) -> pack(List, <<>>).
pack([], Ready) -> Ready;
pack([{Type, Flag}|Rest], Ready) ->
Bit = ?MODULE:Type(Flag) - 1,
Size = size(Ready) * 8,
io:format("switch bit ~p in ~p bits~n", [Bit, Size]),
switch(Bit, Size, Rest, Ready).
-module(base32).
-export([encode/1, decode/1]).
encode(Binary) ->
encode(Binary, <<>>).
encode(<<>>, To) -> To;
encode(<<B:1>>, To) -> <<To/binary, (symbol(B)), "====">>;
encode(<<B:2>>, To) -> <<To/binary, (symbol(B)), "=">>;
encode(<<B:3>>, To) -> <<To/binary, (symbol(B)), "=======">>;
#!/bin/env ruby
require 'fileutils'
DRUPAL_MAJOR = 6
puts "Scanning for available drupal versions"
available = Dir['*'].map do |file|
if file =~ /^drupal-#{DRUPAL_MAJOR}\.(\d+)$/
$1.to_i
@mks-m
mks-m / gist:1827669
Created February 14, 2012 15:46
File.each_lines_batch
class IO
def each_lines_batch(batch_size=1000, buffer_size=4096)
lines = []
chunk = ""
while data = file.read(buffer_size)
chunk << data
lines += chunk.lines.to_a
# last line might not be read until it's end
@mks-m
mks-m / tailor.rb
Created June 21, 2012 18:30
the realtime newrelic
# Usage, assuming that production.log is your rails log
#
# $ tail -f production.log | grep Completed | ruby tailor.rb <estimate> <size>
#
# * estimate is estimated number of requests per second (make it 50% larger)
# * size is length of every stat block(second, minute, hour)
# a line to print out
#
# @accept [Integer] num number of requests
@mks-m
mks-m / graph.rb
Created July 11, 2012 14:59
puppet repo graph
# Generates infrastructure graph in DOT format
# Usage:
# $ find . -name '*.pp' | while read file; do cat $file; echo; done | \
# ruby graph.rb > puppet.dot
NODE = /^\s*(class|define|node)(\s+)([\w:]+)/
RELATION = /^\s*(require|inherits|include)(\s+)([\w:]+)/
USAGE = /^\s*([\w:]*[\w])(\s*)\{(\s*)/
IGNORE = %w{file package service cron exec Exec line plugin}
@mks-m
mks-m / speedtest.rb
Created July 16, 2012 12:50 — forked from equivalent/speedtest
ljust speed test
# just speed test for http://stackoverflow.com/questions/11502629/how-to-do-number-to-string-suffix/11502715#11502715
# if we properly test int<->str conversion the log_10 method is about 30% faster
begin
int = 5
a = Time.now.to_f
10_000_000.times { int.to_s.ljust(3, "0").to_i }
puts Time.now.to_f - a # => 1.3s
create table mc (id int, member varchar(3));
insert into mc values (1, 'abc'), (1, 'pqr'), (2, 'xyz'), (2, 'pqr'), (3, 'pqr'), (3, 'abc');
Select id,count(*) as total from mc group by id having total=(Select count(*) from mc where id=1);
-- Result:
-- +------+-------+
-- | id | total |
-- +------+-------+
@mks-m
mks-m / graph.rb
Last active August 29, 2015 13:57
# Generates infrastructure graph in DOT format
# Usage:
# $ find . -name '*.pp' | while read file; do cat $file; echo; done | \
# ruby graph.rb > puppet.dot
NODE = /^\s*(class|define|node)(\s+)([\w:]+)/
RELATION = /^\s*(inherits|include)(\s+)([\w:]+)/
RELATION2 = /^\s*class(\s+)\{\s*(['"]?)([\w:]+)/
USAGE = /^\s*([\w:]*[\w])(\s*)\{(\s*)/
IGNORE = %w{file package service cron exec Exec line plugin user group class
require 'rubygems'
require 'puppet/util/autoload'
class Puppet::Util::Autoload
class << self
def search_directories(env=nil)
@search_directories ||= {}
# cache only after initialization
if Puppet.settings.app_defaults_initialized?
@search_directories[env] ||= search_directories_uncached(env)