Sha256: 52a606acb9a6b4c864e4872a4754528ff99fbf40d3ab21f2d3f6a4c7fb9153c1

Contents?: true

Size: 975 Bytes

Versions: 10

Compression:

Stored size: 975 Bytes

Contents

# TITLE:
#   Regexp Arity
#
# DESCRIPTION:
#   Matchdata methods to produces a hierachical list of a match result.
#   This also includes Regexp#arity.
#
# AUTHORS:
#   - Thomas Sawyer

#
class Regexp
  # Returns the number of backreferencing subexpressions.
  #
  #   /(a)(b)(c)/.arity  #=> 3
  #   /(a(b(c)))/.arity  #=> 3
  #
  # Note: This is not perfect, especially with regards to \x
  # and embedded comments.

  def arity
    self.source.scan( /(?!\\)[(](?!\?[#=:!>-imx])/ ).length
  end
end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TestRegexpArity < Test::Unit::TestCase

    def test_arity
      r = /(1)(2)(3)/
      assert_equal( 3, r.arity )
      r = /(1)(2)(3)(4)/
      assert_equal( 4, r.arity )
      r = /(1)(2)((a)3)/
      assert_equal( 4, r.arity )
      r = /(?#nothing)(1)(2)(3)(?=3)/
      assert_equal( 3, r.arity )
    end

  end

=end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.0.0 lib/core/facets/regexp/arity.rb
facets-2.0.1 lib/core/facets/regexp/arity.rb
facets-2.0.2 lib/core/facets/regexp/arity.rb
facets-2.0.5 lib/core/facets/regexp/arity.rb
facets-2.1.0 lib/core/facets/regexp/arity.rb
facets-2.1.1 lib/core/facets/regexp/arity.rb
facets-2.1.2 lib/core/facets/regexp/arity.rb
facets-2.0.3 lib/core/facets/regexp/arity.rb
facets-2.0.4 lib/core/facets/regexp/arity.rb
facets-2.1.3 lib/core/facets/regexp/arity.rb