# KennySpeak translator - see http://www.namesuppressed.com/kenny/ #= Usage #== standalone # ruby kennyspeak.rb [-h] [-d] [ ...] # encodes to stdout the specified files (defaults to stdin) # use the -d switch to decode #== library # require 'kennyspeak' # Kenny.encode('kikoo !!') # => "pmpmffpmpppfppf !!" # Kenny.decode('pmpmffpmpppfppf !!') # => "kikoo !!" # #Author:: Yoann Guillot (mailto:john at ofjj.net) #License:: Distributes under the same terms as Ruby module Kenny # This constant is a hash mapping the translation # beetween normal alphabet and kenny's # key: clear text, value: kenny's Base = (0..25).inject(Hash.new) { |h, i| # kennyspeak's letters are just normal alphabet letter index in base 3 trad = i.to_s(3).rjust(3, '0').tr('012', 'mpf') h[(i + ?a).chr] = trad h[(i + ?A).chr] = trad.capitalize h } # returns the encoded version of its argument # unhandled characters are left untouched def self.encode(str) str.gsub(/[a-zA-Z]/) { |k| Base[k] } end # returns the decoded version of its argument # unhandled characters are left untouched # invalid value for a kennychar is translated to +'?'+ def self.decode(str) str.gsub(/[mpf]{3}/i) { |k| Base.index(k) or '?' } end end if __FILE__ == $0 if ARGV.include? '-h' require 'rdoc/usage' RDoc::usage exit end meth = if ARGV.delete('-d') then :decode else :encode end puts Kenny.send(meth, ARGF.read) end