Skip to content
This repository
Browse code

AS guide: documents Regexp#number_of_captures

  • Loading branch information...
commit 23566b071cf211e36d57b09898675fa73e2e7882 1 parent 547865b
Xavier Noria fxn authored

Showing 1 changed file with 26 additions and 0 deletions. Show diff stats Hide diff stats

  1. +26 0 railties/guides/source/active_support_overview.textile
26 railties/guides/source/active_support_overview.textile
Source Rendered
@@ -1547,6 +1547,32 @@ The method +with_indifferent_access+ returns an +ActiveSupport::HashWithIndiffer
1547 1547 {:a => 1}.with_indifferent_access["a"] # => 1
1548 1548 </ruby>
1549 1549
  1550 +h3. Extensions to +Regexp+
  1551 +
  1552 +h4. +number_of_captures+
  1553 +
  1554 +The method +number_of_captures+ returns the number of capturing groups in a given regexp:
  1555 +
  1556 +<ruby>
  1557 +%r{}.number_of_captures # => 0
  1558 +%r{.(.).}.number_of_captures # => 1
  1559 +%r{\A((#)(\w+|\s+))\z}.number_of_captures # => 3
  1560 +</ruby>
  1561 +
  1562 +Routing code for example uses that method to generate path recognizers:
  1563 +
  1564 +<ruby>
  1565 +def recognition_extraction
  1566 + next_capture = 1
  1567 + extraction = segments.collect do |segment|
  1568 + x = segment.match_extraction(next_capture)
  1569 + next_capture += segment.number_of_captures
  1570 + x
  1571 + end
  1572 + extraction.compact
  1573 +end
  1574 +</ruby>
  1575 +
1550 1576 h3. Extensions to +Range+
1551 1577
1552 1578 ...

0 comments on commit 23566b0

Please sign in to comment.
Something went wrong with that request. Please try again.