Skip to content

Commit

Permalink
make ./goruby -e 'p "abc".gs(/a/) { $& }' prints "abc".
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22990 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
akr committed Mar 17, 2009
1 parent a28532d commit c5501b8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions golf_prelude.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit c5501b8

Please sign in to comment.