diff options
author | eban <eban@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-01-14 13:35:51 +0000 |
---|---|---|
committer | eban <eban@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-01-14 13:35:51 +0000 |
commit | c8e8fa7d9134f3a54e3035170d7846923d6212c5 (patch) | |
tree | 2e46016ed73c73717ebe1a7fa46e0c96f2cfc1ea | |
parent | 5f9986c6d22515f325d22fa6cebdc4bf8fc5b823 (diff) | |
download | ruby-c8e8fa7d9134f3a54e3035170d7846923d6212c5.tar.gz ruby-c8e8fa7d9134f3a54e3035170d7846923d6212c5.tar.xz ruby-c8e8fa7d9134f3a54e3035170d7846923d6212c5.zip |
* 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 <darrenks AT ml1.net>.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@15046 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | golf_prelude.rb | 77 |
2 files changed, 78 insertions, 8 deletions
@@ -1,3 +1,12 @@ +Mon Jan 14 22:25:02 2008 WATANABE Hirofumi <eban@ruby-lang.org> + + * 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 <darrenks AT ml1.net>. + Mon Jan 14 21:10:02 2008 Yukihiro Matsumoto <matz@ruby-lang.org> * enc/us_ascii.c: wrong alias name: ANSI_X3.4-1986. diff --git a/golf_prelude.rb b/golf_prelude.rb index 0e356b253..f0c488aba 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)<<i + end + end + def self.const_missing c - r = /^#{c}/ - t = constants.sort.find{|e|r=~e} and return const_get(t) + t = @@golf_hash[ [m,self.class] ] ||= matching_methods(c,constants)[0] + t and return const_get(t) raise NameError, "uninitialized constant #{c}", caller(1) end + def shortest_abbreviation(s='', m=callable_methods) + s=s.to_s + our_case = (?A..?Z)===s[0] + if m.index(s.to_sym) + 1.upto(s.size){|z|s.scan(/./).combination(z).map{|trial| + next unless ((?A..?Z)===trial[0]) == our_case + trial*='' + return trial if matching_methods(trial,m)[0].to_s==s + }} + else + nil + end + end + + def callable_methods + self.class == Object ? methods + private_methods : methods + end + + private + def h(a='H', b='w', c='!') puts "#{a}ello, #{b}orld#{c}" end alias say puts + + def do_while + 0 while yield + end + + def do_until + 0 until yield + end end class Array + alias old_to_s to_s alias to_s join end class FalseClass + alias old_to_s to_s def to_s "" end @@ -40,4 +72,33 @@ end class String alias / split + + def to_a + split('') + end + + (Array.instance_methods-instance_methods-[:to_ary,:transpose,:flatten,:flatten!,:compact,:compact!,:assoc,:rassoc]).each{|meth| + eval"def #{meth}(*args, &block) + a=to_a + result = a.#{meth}(*args, &block) + replace(a.join) + if result.class == Array + Integer===result[0] ? result.pack('c*') : result.join + elsif result.class == Enumerable::Enumerator + result.map(&:join).to_enum + else + result + end + end" + } +end + +class Enumerable::Enumerator + alias old_to_s to_s + (Array.instance_methods-instance_methods-[:replace]+[:to_s]).each{|meth| + eval"def #{meth}(*args, &block) + to_a.#{meth}(*args, &block) + end" + } + alias inspect old_to_s end |