1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

merges r22990 from trunk into ruby_1_9_1.

--
make ./goruby -e 'p "abc".gs(/a/) { $& }' prints "abc".

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_1@23218 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
yugui 2009-04-19 13:33:26 +00:00
parent 22bdfe1413
commit 075c41af3b

View file

@ -2,13 +2,20 @@ class Object
@@golf_hash = {}
def method_missing m, *a, &b
t = @@golf_hash[ [m,self.class] ] ||= matching_methods(m)[0]
t ? __send__(t, *a, &b) : super
if t && b
__send__(t, *a) {|*args|
b.binding.eval("proc{|golf_matchdata| $~ = golf_matchdata }").call($~) if $~
b.call(*args)
}
else
t ? __send__(t, *a, &b) : super
end
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
i.to_s.match(r).captures.map(&:size) << i
end
end