summaryrefslogtreecommitdiffstats
path: root/golf_prelude.rb
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-17 06:40:20 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-17 06:40:20 +0000
commit346157ffb52a5bdd2789c540406e1d1ae0fdfc79 (patch)
tree896c5cde8194757646efc317ebc1af41923ea1d0 /golf_prelude.rb
parenta2fe450b63bb1d45eeb41c3fa50388d1101aa551 (diff)
downloadruby-346157ffb52a5bdd2789c540406e1d1ae0fdfc79.tar.gz
ruby-346157ffb52a5bdd2789c540406e1d1ae0fdfc79.tar.xz
ruby-346157ffb52a5bdd2789c540406e1d1ae0fdfc79.zip
make ./goruby -e 'p "abc".gs(/a/) { $& }' prints "abc".
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@22990 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'golf_prelude.rb')
-rw-r--r--golf_prelude.rb11
1 files changed, 9 insertions, 2 deletions
diff --git a/golf_prelude.rb b/golf_prelude.rb
index 8aa70d833..4363b496f 100644
--- a/golf_prelude.rb
+++ b/golf_prelude.rb
@@ -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