Class: Regexp

Inherits:
Object show all
Defined in:
lib/nano/regexp/arity.rb,
lib/nano/regexp/to_re.rb,
lib/nano/regexp/to_regexp.rb

Instance Method Summary collapse

Instance Method Details

#arityObject

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.



9
10
11
# File 'lib/nano/regexp/arity.rb', line 9

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

#to_re(esc = false) ⇒ Object

Simply returns itself. Helpful when converting strings to regular expressions, where regexp might occur as well –in the same vien as using #to_s on symbols. The parameter is actaully a dummy parameter to coincide with String#to_re.

/abc/.to_re  #=> /abc/


11
12
13
# File 'lib/nano/regexp/to_re.rb', line 11

def to_re( esc=false )
  self  # Of course, things really should know how to say "I" ;)
end

#to_regexpObject

Like #to_re, but following Ruby’s formal definitions, only a Regular expression type object will respond to this.

Note: To be of much real use this should be defined in core Ruby.



7
8
9
# File 'lib/nano/regexp/to_regexp.rb', line 7

def to_regexp
  self
end