From: James Edward Gray II Date: 2007-01-22T05:22:53+09:00 Subject: Fwd: Please Forward: Rubyquiz Submission (CORRECTED) --Apple-Mail-2--22272212 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Begin forwarded message: > From: Phil Darnowsky > Date: January 20, 2007 3:31:36 AM CST > To: submission@rubyquiz.com > Subject: Please Forward: Rubyquiz Submission (CORRECTED) > > Actually, I saw that the two assertions that my original version > failed > are part of the problem definition, and so here is a corrected > version. > Now I am really going to bed. > > > > ______________________________________________________________________ > ______________ > It's here! Your new message! > Get new email alerts with the free Yahoo! Toolbar. > http://tools.search.yahoo.com/toolbar/features/mail/ --Apple-Mail-2--22272212 Content-Transfer-Encoding: 7bit Content-Type: application/x-ruby; x-unix-mode=0666; name=method_abbreviate.rb Content-Disposition: attachment; filename=method_abbreviate.rb class AmbiguousAbbreviationException < Exception # @candidates should get an array of symbols that the abbreviation # might expand to. def initialize(*candidates) super @candidates = candidates end attr_reader :candidates end class Module def abbrev(*meths) # Do abbrev setup once. unless (@abbrev_setup_done) then @abbrev_expansions = Hash.new([]) self.class.send(:attr_reader, :abbrev_expansions) @abbrev_targets = [] module_eval do def method_missing(symbol, *args) candidates = self.class.abbrev_expansions[symbol.to_sym].dup candidates.delete_if {|meth| !(respond_to?(meth))} case when candidates.size == 0: raise(NoMethodError) when candidates.size == 1: send(candidates.first, *args) else # To pass test suite, comment out the raise in # the next line and un-comment the line after that. # I think this way is better. raise(AmbiguousAbbreviationException.new(candidates)) #candidates end end end @abbrev_setup_done = true end # Build hash of possible expansions. module_eval do @abbrev_targets += meths meths.each do |meth| sym = meth.to_sym prefix = "#{meth.to_s}" prefix.chop! while (prefix.length > 0) do prefix_sym = prefix.to_sym unless @abbrev_targets.include?(prefix_sym) then @abbrev_expansions[prefix_sym] += [sym] end prefix.chop! end end end end end --Apple-Mail-2--22272212 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed --Apple-Mail-2--22272212--