From: Daniel Finnie Date: 2007-01-22T10:49:23+09:00 Subject: Re: [QUIZ] Method Auto Completion (#110) --Boundary_(ID_ad08IDwueji2uUC1qqOLuw) Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: 7BIT This is my solution. Probably my fastest and shortest ruby quiz solution, but no less fun! Props to the creator of this quiz. In technicolor glory: http://pastie.caboo.se/34720 Also attached. And pasted below. class Object def method_missing(method, *args, &blk) # Gather all possible methods it could be. possibleMethods = self.methods.select {|x| x =~ /^#{Regexp.escape(method.to_s)}/ } case possibleMethods.size # No matching method. when 0 raise NoMethodError.new("undefined method `#{method}' for #{self.inspect}:#{self.class.name}") # One matching method, call it. when 1 method = possibleMethods.first self.send(method, *args, &blk) # Multiple possibilities, return an array of the possibilities. else possibleMethods end end end Ruby Quiz wrote: > The three rules of Ruby Quiz: > > 1. Please do not post any solutions or spoiler discussion for this quiz until > 48 hours have passed from the time on this message. > > 2. Support Ruby Quiz by submitting ideas as often as you can: > > http://www.rubyquiz.com/ > > 3. Enjoy! > > Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone > on Ruby Talk follow the discussion. Please reply to the original quiz message, > if you can. > > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= > > by Robert Dober > > Command Line Interfaces very often support command abbreviations The purpose of > this quiz is to automatically dispatch to methods on unambiguous abbreviations, > ask the user for clarification in case of ambiguous abbreviations and raise a > NoMethodError in case of an abbreviation that cannot be matched to any command. > > Behavior of other methods defined in a class shall not be altered. > > Be creative about the interface and about behavior. I have OTOH defined a small > test suite that makes assumptions about the interface and behavior. But the test > suite is only there for your convenience. > > What is said below applies to the test suite and shall in no way inhibit any > alternative ideas. > > class Mine > abbrev :step, :next, :stop > abbrev :exit > end > > Mine.new.e # should resolve to exit > Mine.new.st # should prompt the user > Mine.new.a # should still raise a NoMethodError > > Abbreviation targets themselves are not expanded. > > class Nine > abbrev :hash > abbrev :has > end > > Nine.new.ha # => [:hash, :has] > Nine.new.has # => NoMethodError > > class Nine > def has; 42; end > end > Nine.new.has # => 42 > > In order to allow for automated testing the test code shall not prompt the user > in case of an ambiguous abbreviation but return an array containing all (and > only all) possible completions as symbols. Note that the test suite sets the > global variable $TESTING to a true value for your convenience. > > http://rubyquiz.com/test-abbrev.rb > > --Boundary_(ID_ad08IDwueji2uUC1qqOLuw) Content-type: application/x-ruby; name=method_completion.rb Content-transfer-encoding: base64 Content-disposition: inline; filename=method_completion.rb Y2xhc3MgT2JqZWN0CglkZWYgbWV0aG9kX21pc3NpbmcobWV0aG9kLCAqYXJncywgJmJsaykK CQkjIEdhdGhlciBhbGwgcG9zc2libGUgbWV0aG9kcyBpdCBjb3VsZCBiZS4KCQlwb3NzaWJs ZU1ldGhvZHMgPSBzZWxmLm1ldGhvZHMuc2VsZWN0IHt8eHwgeCA9fiAvXiN7UmVnZXhwLmVz Y2FwZShtZXRob2QudG9fcyl9LyB9CgkJCgkJY2FzZSBwb3NzaWJsZU1ldGhvZHMuc2l6ZQoJ CSMgTm8gbWF0Y2hpbmcgbWV0aG9kLgoJCXdoZW4gMAoJCQlyYWlzZSBOb01ldGhvZEVycm9y Lm5ldygidW5kZWZpbmVkIG1ldGhvZCBgI3ttZXRob2R9JyBmb3IgI3tzZWxmLmluc3BlY3R9 OiN7c2VsZi5jbGFzcy5uYW1lfSIpCgkJCgkJIyBPbmUgbWF0Y2hpbmcgbWV0aG9kLCBjYWxs IGl0LgoJCXdoZW4gMQoJCQltZXRob2QgPSBwb3NzaWJsZU1ldGhvZHMuZmlyc3QKCQkJc2Vs Zi5zZW5kKG1ldGhvZCwgKmFyZ3MsICZibGspCgkJCgkJIyBNdWx0aXBsZSBwb3NzaWJpbGl0 aWVzLCByZXR1cm4gYW4gYXJyYXkgb2YgdGhlIHBvc3NpYmlsaXRpZXMuCgkJZWxzZQoJCQlw b3NzaWJsZU1ldGhvZHMKCQllbmQKCWVuZAplbmQ= --Boundary_(ID_ad08IDwueji2uUC1qqOLuw)--