Skip to content

Instantly share code, notes, and snippets.

@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
@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
{ "!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
@trans
trans / nilcomparable.rb
Created May 2, 2012 06:04
Make NilClass comparable
# This tiny framework simply makes nil comparable, such that
# all things (except itself) are greater than it.
#
# The main effect is this:
#
# nil <=> any_object #=> -1
#
# To enable a class to compare against nil (reverse of the above),
# simply prepend a nil comparsion into the #<=> method of that class.
# For example:
@trans
trans / ruby-build-1.9.3-rc1+tk8.5.sh
Created October 27, 2011 14:56
ruby-build, install latest Ruby with Tk
if [ -z $installdir ]; then
installdir="/opt/ActiveTcl-8.5"
fi
if [ ! -d "$installdir" ]; then
echo "Installing ActiveTcl8.5.10..."
echo "IMPORTANT: Be sure to use the default settings in the install dialog."
wget http://downloads.activestate.com/ActiveTcl/releases/8.5.10.1/ActiveTcl8.5.10.1.295062-linux-x86_64-threaded.tar.gz
tar -xf ActiveTcl8.5.10.1.295062-linux-x86_64-threaded.tar.gz