Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Port a Library (#64)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
amrangaye  
View profile  
 More options Jan 30 2006, 11:52 am
Newsgroups: comp.lang.ruby
From: "amrangaye" <amrang...@gmail.com>
Date: 30 Jan 2006 08:52:40 -0800
Local: Mon, Jan 30 2006 11:52 am
Subject: Re: Port a Library (#64)
Hello.

This is my first rubyquiz, and I am still learning Ruby. I decided to
go with something simple but fun. So I did a search on the CPAN (I've
used Perl before) for the Acme modules, and chose Acme::Bleach
(http://search.cpan.org/~dconway/Acme-Bleach-1.12/lib/Acme/Bleach.pm)
to implement. I couldn't find a Ruby version on either RAA or
rubyforge.

Acme::Bleach is a module by Damian Conway, and it literally bleaches
your program, whilst still leaving it in a runnable state. It's a
really cool little module, and I stuck as close to the original as
possible - even using nearly the same method names. Here it is in its
entirety. Any suggestions, criticisms, etc. are highly welcome.

#Ruby port of Acme::Bleach - by Amran Gaye
#You can use this by doing an "include 'Bleach' " at the top of your
program

$tie="\t"*8

def whiten(laundry)
  #Change laundry to binary 1s and 0s...
  #then change those to tab and space characters. Finally add newlines
after every 9th character
  result = laundry.unpack('b*').to_s.tr('01',"
\t").gsub(/(.{9})/,"\\1\n")
  return $tie + result        #Add a tie to the washed shirt, and
return it
end

def brighten(laundry)
  #Does the opposite of whiten
  laundry.sub!(/\t{8}/,'')    #Remove tie
  laundry.tr!("\n",'')    #Remove newlines
  laundry.tr(" \t",'01').to_a.pack('b*') #Change spaces and tabs to 0s
and 1s, then repack them as binary
end

def dirty?(laundry)           #Laundry is dirty only if it contains
non-space characters
  laundry =~ /\S/
end

def proper?(laundry)               #shirt is proper if it contains a
tie
  laundry =~ /^#$tie/
end

shirt = IO.readlines($0).to_s          #Read in current program
shirt.sub!("require 'Bleach'",'')      #Remove require line

if(not dirty?(shirt) and proper?(shirt))
  eval brighten(shirt)
else
  file = File.new($0,"w")
  file.puts("require 'Bleach'")
  file.puts(whiten(shirt))
  file.close
end


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.