Skip to content

Instantly share code, notes, and snippets.

@trans
trans / better.ini
Created August 6, 2012 20:48
INI files with sub-hierarchies
[contact]
email.account = transfile@gmail.com
email.host = mail.gmail.com
email.login = true
@trans
trans / email-parser.rb
Created December 10, 2017 01:51
Parse Email Crude
class Email
attr :headers
attr :body
def initialize
@headers = Multimap.new
@body = ""
end
def parse(io)
@trans
trans / array_zip.rb
Created August 23, 2011 18:00
Able to use block with zip
class Array
def zip(a, &b)
if b
r = []
bs = lambda{ |x| r << b.call(*x) }
super(a, &bs)
return r
else
super(a)
@trans
trans / reflect.rb
Created February 2, 2014 17:08
Very simply implementation of object reflection.
# NOTE: This code was part of Ruby Facets standard library, but
# it has been deprecated in favor of more sophisticated
# implementations, such as the `instance` and `mirror` gems.
module ObjectSpace
# Reflection ensures that information about an object
# is actual according to Ruby's Kernel definitions, just
# in case such methods have been overridden.
#
@trans
trans / binbahn.sh
Created February 18, 2013 15:38
Add paths in ./bin to $PATH. Seemed like a neat idea --but not so much.
export ROOTPATH="$PATH"
function cd() {
builtin cd "$@"
for f in ./bin/*
do
export PATH="$f:$ROOTPATH"
done
}
@trans
trans / crypt3.rb
Last active December 11, 2015 11:39
A ruby version of crypt(3), a salted one-way hashing of a password.
# Crypt3
#
# A ruby version of crypt(3), a salted one-way hashing of a password.
#
# The Ruby version was written by Poul-Henning Kamp.
#
# Copyright (c) 2002 Poul-Henning Kamp
#
# Adapted by guillaume__dot__pierronnet_at__laposte__dot_net based on
# * http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/325204/index_txt
@trans
trans / identity.rb
Created December 2, 2012 01:31
Make some code identity and churn out a mess of equivalent code.
class SimpleCodeGenerator
def self.identities
@identities ||= []
end
def self.identity(&block)
identities << block
end
{ "!records": [
{ "!record": {
"game": { "!game &001": {
"date": {"!!date": "March 2, 1962"},
"versus": "New York"
}},
"notes": "!!! Awesome !!!",
"number": 100,
"player": { "!player &002": {
"name": "Wilt Chamberlain",
{{! this is a specific (dummy) page with a menu in the 'portal' application}}
{{. portal/prototypes/menuPage.html}}
{{=pageId}}aboutMe{{/pageId}}
{{=pageTitle}}
{{#i18n}}aboutMe.title{{/i18n}}
{{/pageTitle}}
{{=pageContent}}
{{#i18n}}aboutMe.title{{/i18n}}
@trans
trans / bench_index.rb
Created July 31, 2012 04:58
Benchmark iteration with index approaches.
require 'benchmark'
# Each
Benchmark.bmbm do |x|
n = 1000000
a = ('a'..'z').to_a
x.report("each") do
n.times { a.each{ |e| e } }
end