From: ts Date: 2003-04-08T01:06:20+09:00 Subject: Re: a question about regexp >>>>> "B" == Ben Thomas writes: B> Displaying $& in the inner loop of grep works fine, but when it enter my own B> block, $& is put back to nil. Is there a way to keep the value of $& (and B> all others $1...)? Well, $~ is a local, and thread local variable and perhaps in your case it best to give this variable to the block, something like def mygrep( exp ) ar = [ "element1", "element2" ] ar.grep( exp ) { |e| yield e, $~ } end mygrep( /(.*)/ ) { |e, match| # doesn't work... print "mygrep: #{match[0]}\n" } $~ is an object MatchData pigeon% ri MatchData ------------------------------------------------------------------------ class: MatchData ------------------------------------------------------------------------ MatchData is the type of the special variable $~, and is the type of the object returned by Regexp#match and Regexp#last_match. It encapsulates all the results of a pattern match, results normally accessed through the special variables $&, $', $`, $1, $2, and so on. ------------------------------------------------------------------------ [], begin, end, length, offset, post_match, pre_match, size, string, to_a, to_s ------------------------------------------------------------------------ pigeon% Guy Decoux