From: "Vincent L." Date: 2008-06-04T19:08:14+09:00 Subject: Re: instance_eval, regexp with block : losing $1 I finally managed to get it to work : class Base def self.match(name, regexp, &block) define_method(name) do |text| puts "#{text}" text.gsub!(regexp) do #hack to restore magical $~ (and so $1, $2, etc.) in block eval('lambda{|x| $~ = x}', block).call($~) block.call #or instance_eval(&block) end puts "=> #{text}" end end end class Foo < Base match :g, /(.)g$/i do $1.upcase + '_ggg' end end f = Foo.new f.g('bla') f.g('doing') -- Posted via http://www.ruby-forum.com/.