Re:New Ruby 1.9 Features, Tips & Tricks
by igrigorik on Monday February 21, 2011 @03:43PM
(#2247084)
Speaking of named captures.. Something I didn't mention in the blog post, but a rather handy feature:
ruby-1.9.2-p136 :011 > /My name is (?<first>\w+) (?<last>\w+)/ =~ "My name is Ilya Grigorik"
ruby-1.9.2-p136 :012 > first
"Ilya"
ruby-1.9.2-p136 :013 > last
"Grigorik"
> Currently, find/replace only lets you re-order or remove groups or insert static text.
I must be misunderstanding you, but that's obviously incorrect.
"foo 1 bar 2".gsub(/\d+/) {|m| m.to_i * 2 }
1.8 also. Does MatchEvaluator bring more to the table?
.Net has had them for a long, long time and they are AWESOME!
It'd be cool to see Ruby get something like a MatchEvaluator, too
http://msdn.microsoft.com/en-us/library/system.text.regulare...
A MatchEvaluator lets you much more complicated regex find replace. Currently, find/replace only lets you re-order or remove groups or insert static text. A MatchEvaluator lets you handle what to replace the match with code - meaning you can call a webservice that will replace MM/DD/YY with the equivalent Mayan/Persian/Lunar date.