2007-10-06 @ 21:55
Ruby Bleach
I’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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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:
After you run it, your program will be bleached! yay! Thanks Damian!