Skip to content

Instantly share code, notes, and snippets.

@rubyworks
rubyworks / semaphore.rb
Created April 27, 2010 12:17
semaphore.rb
# = Semaphore
#
# Technically a semaphore is simply an integer variable which
# has an execution queue associated with it.
#
# Copyright (c) 2005 Fukumoto
class Semaphore
def initialize(initvalue = 0)
@rubyworks
rubyworks / roman_numeral.rb
Created March 2, 2010 17:03
RomanNumeral Class
# Work with Roman numerals just like normal Integers.
class RomanNumeral < Numeric
include Comparable
# The largest integer representable as a roman
# numerable by this module.
MAX = 3999
# Taken from O'Reilly's Perl Cookbook 6.23. Regular Expression Grabbag.
REGEXP = /^M*(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])$/i
@rubyworks
rubyworks / instance_eval.rb
Created February 27, 2010 04:30
instance_eval.rb
require 'facets/functor'
class Object
# Use #instance_eval with a fluid notation.
#
# class X
# attr :a
# private :a
# def initialize
@rubyworks
rubyworks / oneliner_santa.rb
Created February 23, 2010 14:59
Oneliner - Santa Claus
loop{7.times{|i|puts"\e[2J\e[0;3H_...\n / _ '.\n|.'(@@@@@)\n(*)/ %s \\
| .o. | \n ( %s ) %s"%[i==5?"- -":"e e",(i+3)%7<4?"=":"o",i>0?"-"+
(" Ho"*i)[0,9]+"!":""];puts" ( )\n"*2," ( )\n"*3,' "-"';
sleep([5,3,3,4,5,1,4][i]/6.0)}}
@rubyworks
rubyworks / oneliner_rubyquiz.rb
Created February 23, 2010 14:58
Oneliner - Ruby Quiz Banner
"bp6siZmijp5CiZlCiW5CgAAChpbiiZYiiZZCi5aCZ2bs".unpack("m")[0].unpack("C*").map{|x|
x.chr}.join.unpack("B*")[0].scan(/.{24}/){i=7;$&.scan(/..../){
print"\e[3#{i-=1};1;40m  ";$&.each_byte{|z|print" #"[z-?0,1]*2}};puts"\e[0m"}
@rubyworks
rubyworks / oneliner_fractaltree.rb
Created February 23, 2010 14:57
Oneliner - Fractal Tree
n=32;l="A";n.times{|y|print" "*(n-1-y),(0..y).map{|x|~y&x>0?" .":" #{l}"},$/}
@rubyworks
rubyworks / oneliner_pascal.rb
Created February 23, 2010 14:56
Oneliner - Pascal's Triangle
a=[];10.times{|n|a<<1;puts" "*(9-n)*3+"%6d"*-~n%a;n.times{|i|a[n]+=a[n-=1]}}
a=[];10.times{|n|a<<1;puts" "*(9-n)*3+"%6d"*(n+1)%a;n.times{|i|a[n]+=a[n-=1]}}
@rubyworks
rubyworks / oneliner_duck1.rb
Created February 23, 2010 14:52
Oneliner - Duck 1
s="\033[2J\033[0;0H _\n Quack! >(')____,\n (` =~~/\n~^~^~^~^~^`---'~^~^~^~^";
(1..(1/0.0)).each{|i|s[23,6]=(i%2)==0?"Quack!":" "*6;s.tr!('>~^^~<','<^~~^>');puts s;sleep 1}
@rubyworks
rubyworks / oneliner_ruby.rb
Created February 23, 2010 14:52
Oneliner - Ruby Banner
"jp6iSZmkLp5ISZlEiW5C".unpack("m")[0].unpack("C*").map{|x|x.chr}.join.
unpack("B*")[0].scan(/.{24}/){i=7;$&.scan(/..../){print"\e[3#{i-=1};1;40m ";
$&.each_byte{|z|print" #"[z-?0,1]*2}};puts"\e[0m"}
@rubyworks
rubyworks / pc.rb
Created February 1, 2010 08:53
Inspect and View Caller
def pc(*args)
super *(args << caller[0])
end