News for hackers. Stuff that matters.
Re:New Ruby 1.9 Features, Tips & Tricks by viggity on Monday February 21, 2011 @02:55PM (#2246888)
Named captures are a great way to document your regular expressions. Don't use the "#" notation to comment on what the regex is doing, just name the group instead!

.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.

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"

Re:New Ruby 1.9 Features, Tips & Tricks by rue on Monday February 21, 2011 @04:55PM (#2247392)
> 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?
Re:New Ruby 1.9 Features, Tips & Tricks by viggity on Monday February 21, 2011 @10:41PM (#2248610)
that looks exactly like a match evaluator, so it looks like ruby does have that ability. Awesome.