From: Robert Klemme Date: 2010-11-05T19:30:56+09:00 Subject: [ruby-core:33064] Re: Suggestion for MatchData#first and #last On Fri, Nov 5, 2010 at 11:21 AM, Ammar Ali wrote: > On Fri, Nov 5, 2010 at 11:34 AM, Robert Klemme > wrote: >> Hi, >> >> I frequently do regexp matching with several alternatives where it >> does not matter which group matches, e.g. >> >> md = /foo=(\S+)|bar\[([^]]+)\]/.match input >> >> For these situations it would be convenient to have something like >> this in MatchData >> >> class MatchData >> �# Return the first capturing group that matched >> �def first_group # or #first but this might collide with Array#first >> � �for i in 1...size >> � � �group = self[i] and return group >> � �end >> � �nil >> �end >> >> �# Return the last capturing group that matched >> �def last_group # or #last but this might collide with Array#last >> � �(size - 1).downto 1 do |i| >> � � �group = self[i] and return group >> � �end >> � �nil >> �end >> end >> >> I propose to add that to the std lib. �What do others think? > > Isn't md.captures.first and md.captures.last sufficient? It's not > exactly the same functionality, but close, right? I'm not voting, yet, > but trying to understand what's new here. No, it's not even close. robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/