Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

AS guide: documents Regexp#number_of_captures

  • Loading branch information...
commit 23566b071cf211e36d57b09898675fa73e2e7882 1 parent 547865b
@fxn fxn authored
Showing with 26 additions and 0 deletions.
  1. +26 −0 railties/guides/source/active_support_overview.textile
View
26 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
</ruby>
+h3. Extensions to +Regexp+
+
+h4. +number_of_captures+
+
+The method +number_of_captures+ returns the number of capturing groups in a given regexp:
+
+<ruby>
+%r{}.number_of_captures # => 0
+%r{.(.).}.number_of_captures # => 1
+%r{\A((#)(\w+|\s+))\z}.number_of_captures # => 3
+</ruby>
+
+Routing code for example uses that method to generate path recognizers:
+
+<ruby>
+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
+</ruby>
+
h3. Extensions to +Range+
...
Please sign in to comment.
Something went wrong with that request. Please try again.