From: James Edward Gray II Date: 2006-04-24T04:49:27+09:00 Subject: Fwd: [SOLUTION] Text Munger --Apple-Mail-3-10839432 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Begin forwarded message: > From: "Jason Zaugg" > Date: April 23, 2006 9:29:27 AM CDT > To: submission@rubyquiz.com > Subject: [SOLUTION] Text Munger > > Can you please forward this solution to the list? > > This was a nice bite-sized quiz that encouraged me to participate > for the first time and learn a thing or two. Hopefully others will > too. > > In deference to the style of the solutions I've read since > discovering Ruby Quiz recently my solution includes obligatory (if > gratuitous) use of (method|const)_missing :) > > Tkhnas! > > Jason Zaugg --Apple-Mail-3-10839432 Content-Transfer-Encoding: 7bit Content-Type: application/binary; x-unix-mode=0666; name="text_munger.rb" Content-Disposition: attachment; filename=text_munger.rb require 'uri' require 'open-uri' require 'rexml/document' class Array def shuffle! original = dup size.times {|i| self[i] = original.delete_at(rand(original.size))} end end module TextMunger PLACE_HOLDER = "\0" ALPHA = "a-zA-Z" REGEX = /(^[^#{ALPHA}]*[#{ALPHA}])(.*?)([#{ALPHA}][^#{ALPHA}]*$)/ def self.munge_text(text) text.split(/\b/).collect {|w| munge_word(w)}.join end def self.find_unambiguous_match(munged, anEnumerable) matches = anEnumerable.find_all { |x| eq_munged(x, munged.to_s) } matches.size == 1 ? matches.first : nil end private def self.munge_word(word) return word unless word =~ REGEX head, mid, tail = $~.captures chars = [] mid_template = mid.gsub(/[#{ALPHA}]/) { |c| chars << c; PLACE_HOLDER } template = [head, mid_template, tail].join chars.shuffle! template.gsub(/#{PLACE_HOLDER}/) { chars.pop } end def self.eq_munged(word, other_word) word.split(//).sort == other_word.split(//).sort and md = word.match(REGEX) and other_md = other_word.match(REGEX) and [md.captures.first, md.captures.last] == [other_md.captures.first, other_md.captures.last] end end class Object alias method_missing_orig method_missing def method_missing(sym, *args, &block) all_methods = [public_methods, protected_methods, private_methods].flatten if method = TextMunger::find_unambiguous_match(sym, all_methods) send(method, *args, &block) else method_missing_orig(sym, *args, &block) end end end class Module alias const_missing_orig const_missing def const_missing(sym) if constant = TextMunger::find_unambiguous_match(sym, constants) module_eval(constant) else const_missing_orig(sym) end end end def get_text url = "http://www.rubyquiz.com/quiz76.html" xml = open(url) do |f| doc = REXML::Document.new(f) nodes = doc.elements.to_a('//div[@id="content"]//') text_blocks = nodes.collect {|a| a.texts.collect {|t| t.value}.join(" ")} text_blocks.delete_if { |b| b.strip.empty? } end end ptus gtt_xeet.cllocet {|t| TexMtunegr::mgnue_txet(t)}.jion("\n" * 2) --Apple-Mail-3-10839432 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed --Apple-Mail-3-10839432--