This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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). | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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)), "=======">>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | | |
-- +------+-------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer