#-- # One # # Copyright (c) 2005 Thmas Sawyer # # Ruby License # # This module is free software. You may use, modify, and/or redistribute this # software under the same terms as Ruby. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. # #================================================================================ # ChangeLog #================================================================================ # 2004.10.25 trans Initial release. #================================================================================ #++ # :title: One.rb # # One liners helper library provides one letter # alias for a number of classes, modules and # methods, allowing one to create very tight one-liners. # This this bit of a "gag" library, but it can also # be used for code obsfuscation and includes examples # of some neat one liners. # # == Author(s) # # * Thomas Sawyer # K=Kernel O=Object C=Class module Kernel alias pi p alias p print alias ps puts alias s sleep alias l loop end module Enumerable alias m map end class Array alias e each alias r reverse end class String alias e each alias r reverse end class Integer alias t times end module FamousOneLiners extend self ONELINERS = {} def define_oneliner( name, &code_blk ) code = code_blk.call.gsub(/^ */, '').gsub("\n",'') ONELINERS[name.to_sym] = code define_method( name ) eval ONELINERS[name.to_sym] end end def oneliner(name) ONELINERS[name.to_sym] end def oneliners ONELINERS.keys end # Example one-liners define_oneliner :color_ruby do <<-HERE "jp6iSZmkLp5ISZlEiW5C".unpack("m")[0].unpack("C*").map{|x|x.chr}.join. unpack("B*")[0].scan(/.{24}/){i=7;$&.scan(/..../){print"\e[3#{i-=1}; 1;40m ";$&.each_byte{|z|print" #"[z-?0,1]*2}};puts"\e[0m"} HERE end define_oneliner :quack do <<-HERE s="\e[2J\e[0;0H         _\nQuack! >(*)____,\n        (` =~~/\n^v^v^v^v^`---'v^v^v^v^\n";i=0; l{s[21,6]=i&1>0?'Quack!':' '*6;s.tr!(t='*.>v^<,"',t.r);$><0?'Quack!':' '*6;s.tr!(t='*.>v^<,"',t.reverse);$><