Skip to content

Instantly share code, notes, and snippets.

@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 / invoice-A.yes
Created June 28, 2011 16:11
Possible Formats for .yes document using invoice.yml example.
---
/:
tag: <tag:clarkevans.com,2002:invoice>
invoice: &mandatoryint
type: int
required: true
date:
type: date
required: true
bill-to: &billing-schema-alias
@trans
trans / raml.rb
Created May 28, 2011 01:45
Heart of Ruby As Meta-Language
require 'thread'
module RAML
def self.load(io, options={})
case io
when String
file = '(eval)'
code = io
when File
@trans
trans / ansi_test.log
Created May 9, 2011 19:35
If only you could see the color
trans@logisys:ansi$ redline test
ANSI v1.2.6 /home/trans/Documents/programs/github.com/rubyworks/ansi
qed Test
Started
..............................I, [2011-05-09T15:32:44.350689 #15187] INFO -- : Info logs are green.
@trans
trans / recorder.rb
Created April 24, 2011 11:45
Simple method probe.
# Recorder, Copyright (c) 2006 Thomas Sawyer
require 'facets/kernel/object_class'
# = Recorder
#
# The Recorder class is a <i>method probe</i>. It records everything
# that happens to it, building an internal action tree. You can then
# pass a substitute object and apply the recording to it. Or you can
# analyze the action tree.
@trans
trans / bm_or_equals.rb
Created March 4, 2011 15:53
Benchmark reading with and without ||= assignment.
# Run 1000000 Times:
#
# ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
#
# Simple Read : 0.217292 seconds
# Or Equals : 0.31042 seconds
#
# ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
#
# Simple Read : 0.113411725 seconds
# AutoArray
# Copyright (c) 2005 Brian Schröder
# TODO: Use or integrate with SparseArray
# An Array that automatically expands dimensions as needed.
#
# a = Autoarray.new
# a[1][2][3] = 12
# a #=> [nil, [nil, nil, [nil, nil, nil, 12]]]
# a[2][3][4] #=> []
# TITLE:
#
# HashBuilder
#
# DESCRIPTION:
#
# Build a hash programmatically via missing method calls.
#
# COPYRIGHT:
#
@trans
trans / snapshot.rb
Created September 10, 2010 20:09
Michael Neumann's Snapshot
# = Snapshot
#
# A lightweight single-depth object state capture.
# The #take_snapshot method reads the object's state,
# which is generally it's collection of instance variables,
# and returns them in a hash. The state can be restored
# with #apply_snapshot.
#
# == Todo
#
@trans
trans / succ_n.rb
Created August 24, 2010 14:26
Optimize #succ for skipping steps
# The idea here is that #succ could take an optional parameter
# to help optimize certain (albeit rare) cases when skipping steps
# is required.
class Fixnum
# Allows #succ to take _n_ increments.
#
# 3.succ(2) #=> 5
#