Ruby Bleach 2

Posted by Aaron Patterson on October 06, 2007

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:

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!

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Khan Sun, 07 Oct 2007 10:37:17 PDT

    Do you like Ruby? Do you like Bleach? If so, this link’s for you: http://rubyquiz.com/quiz34.html

    Wolverines!!!!!!!!!!!!!!

  2. Aaron Patterson Sun, 07 Oct 2007 11:26:58 PDT

    Ha! Thats what I get for not reading Ruby Quiz. Or googling. :-)

Comments

Check Spelling
Activate Spell Check while Typing