lib/facet/regexp/arity.rb in facets-0.9.0 vs lib/facet/regexp/arity.rb in facets-1.0.0

- old
+ new

@@ -1,40 +1,80 @@ -require 'nano/regexp/arity.rb' \ No newline at end of file +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 TCRegexp < 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