From: James Edward Gray II Date: 2007-01-22T05:22:43+09:00 Subject: Fwd: Please Forward: Ruby Quiz Submission --Apple-Mail-1--22285100 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:03:27 AM CST > To: submission@rubyquiz.com > Subject: Please Forward: Ruby Quiz Submission > > Attached please find my modest solution to #110, which is the first > Rubyquiz I've bothered to submit, though I've been following the site > for about two months now--thanks for the fun challenges! > > This solution doesn't pass the test suite for two reasons: > > (1) I think, instead of just returning an array of possible expansions > in response to an ambiguous abbreviation, the Right Thing is to raise > an exception that carries that array around inside it. This is not > what the test suite is expecting, but of course it's trivial to modify > my solution to do that instead. > > (2) This fails the assertions in line 118 & 131. I think that, even > though @class#aa is not defined at the moment that it's passed to > abbrev, it's valid to allow it to be called through an > abbreviation at > any time after it's defined. You could also make the case, I suppose, > that the abbreviation :a should preclude @class#aa from being invoked > when a.a is called, but I'd consider this a pathological case to start > with. If this is the kinda thing you're going to go around doing, you > shouldn't act surprised when you get weird results. > > Anyhow, I am going to bed--my girlfriend called in the middle of this > e-mail and the first words out of her mouth were "Why aren't you > asleep?" So I think I will. > > Thanks again, > > Phil Darnowsky > > > > ______________________________________________________________________ > ______________ > Cheap talk? > Check out Yahoo! Messenger's low PC-to-Phone call rates. > http://voice.yahoo.com --Apple-Mail-1--22285100 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) 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 raise(AmbiguousAbbreviationException.new(candidates)) end end end @abbrev_setup_done = true end # Build hash of possible expansions. module_eval do meths.each do |meth| sym = meth.to_sym prefix = meth.to_s while (prefix.length > 0) do prefix_sym = prefix.to_sym @abbrev_expansions[prefix_sym] += [sym] prefix.chop! end end end end end --Apple-Mail-1--22285100 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed --Apple-Mail-1--22285100--