From 45432a6d2b22bbc7318819e251127021810a728d Mon Sep 17 00:00:00 2001 From: eban Date: Mon, 14 Jan 2008 13:35:51 +0000 Subject: * golf_prelude.rb: Shorter method name completion. Same method used for const missing. do_while and do_until added. Enumerator gains all of Array's abilities. Ex: '123'.m{|i|i*2} #=> "112233" '123'.pe #=> '123'.perm*' ' #=> "123 132 213 231 312 321" base on a patch from Darren Smith . git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15046 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- golf_prelude.rb | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 8 deletions(-) (limited to 'golf_prelude.rb') diff --git a/golf_prelude.rb b/golf_prelude.rb index 0e356b2537..f0c488abad 100644 --- a/golf_prelude.rb +++ b/golf_prelude.rb @@ -1,33 +1,65 @@ -SCRIPT_LINES__={} - class Object @@golf_hash = {} def method_missing m, *a, &b - t = @@golf_hash.fetch(k = [m,self.class]) do - r = /^#{m.to_s.gsub(/(?<=\w)(?=_)/, '\w*?')}/ - @@golf_hash[k] = (methods + private_methods).sort.find{|e|r=~e} - end + t = @@golf_hash[ [m,self.class] ] ||= matching_methods(m)[0] t ? __send__(t, *a, &b) : super end + def matching_methods(s='', m=callable_methods) + r=/^#{s.to_s.gsub(/./){"(.*?)"+Regexp.escape($&)}}/ + m.grep(r).sort_by do |i| + i.to_s.match(r).captures.map(&:size)<