From 23566b071cf211e36d57b09898675fa73e2e7882 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 26 Sep 2009 03:34:55 +0200 Subject: AS guide: documents Regexp#number_of_captures --- .../guides/source/active_support_overview.textile | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'railties/guides/source') diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index 8e53672abd..89c959c49f 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -1547,6 +1547,32 @@ The method +with_indifferent_access+ returns an +ActiveSupport::HashWithIndiffer {:a => 1}.with_indifferent_access["a"] # => 1 +h3. Extensions to +Regexp+ + +h4. +number_of_captures+ + +The method +number_of_captures+ returns the number of capturing groups in a given regexp: + + +%r{}.number_of_captures # => 0 +%r{.(.).}.number_of_captures # => 1 +%r{\A((#)(\w+|\s+))\z}.number_of_captures # => 3 + + +Routing code for example uses that method to generate path recognizers: + + +def recognition_extraction + next_capture = 1 + extraction = segments.collect do |segment| + x = segment.match_extraction(next_capture) + next_capture += segment.number_of_captures + x + end + extraction.compact +end + + h3. Extensions to +Range+ ... -- cgit v1.2.3