Ruby Bleach
Oct 6, 2007 @ 9:55 pmI’ve been a fan of Damian Conway for a while now. One of my favorite modules he’s written is Acme::Bleach. I thought I’d try writing my own in ruby, and this is what I came up with:
module Acme
class Bleach
TIE = " \t" * 8
def initialize(file)
@shirt = File.open(@file = file, 'rb') { |f|
f.read
}.gsub("require 'bleach'\n", '')
end
def dirty?; @shirt =~ /\S/; end
def dress?; @shirt =~ /^#{TIE}/; end
def whiten
TIE + @shirt.unpack('b*')[0].tr('01', " \t").gsub(/(.{9})/, '\1'+"\n")
end
def brighten
@shirt = [@shirt.gsub(/^#{TIE}|[^ \t]/, '').tr(" \t", '01')].pack('b*')
end
def fold
File.open(@file, 'wb') { |f| f.puts "require 'bleach'"; f.write whiten }
end
end
end
shirt = Acme::Bleach.new($0)
eval(shirt.brighten) unless shirt.dirty? && !shirt.dress?
shirt.fold
Store that in a file named “bleach.rb”. Then in another file do something like this:
require 'bleach'
puts "hello world"
After you run it, your program will be bleached! yay! Thanks Damian!