Class: Regexp

Inherits:
Object show all
Defined in:
lib/quality_extensions/symbol/match.rb,
lib/quality_extensions/regexp/join.rb,
lib/quality_extensions/regexp/to_plain_s.rb,
lib/quality_extensions/regexp/named_captures.rb,
lib/quality_extensions/enumerable/grep_with_index.rb

Overview

returns array with [index (of line/element that matched) , the matched line/element]

Defined Under Namespace

Modules: WithSupportForSymbols

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.join(*elements) ⇒ Object

Returns a Regexp that results from interpolating each of the given elements into an empty regular expression.

/ab/ == Regexp.join(/a/, /b/)  # except spelled differently

Accepts both strings and Regexp’s as elements to join together. Strings that are passed in will be escaped (so characters like ‘*’ will lose all of the Regexp powers that they would otherwise have and are treated as literals).

Serving suggestion: Use it to check if the actual string in an assert_match contains certain literal strings, which may be separated by any number of characters or lines that we don’t care about. In other words, use it to see if a string contains the necessary “keywords” or “key phrases”…

assert_match Regexp.join(
  'keyword1',
  /.*/m,
  'keyword2'
), some_method()
# where some_method() returns "keyword1 blah blah blah keyword2"


27
28
29
30
31
# File 'lib/quality_extensions/regexp/join.rb', line 27

def self.join(*elements)
  elements.inject(//) do |accumulator, element|
    accumulator + element
  end
end

.loose_join(*elements) ⇒ Object

Pads the elements (which may be strings or Regexp’s) with /.*/ (match any number of characters) patterns. Pass :multi_line => true if you want /.*/m as the padding pattern instead.



35
36
37
38
39
40
41
42
# File 'lib/quality_extensions/regexp/join.rb', line 35

def self.loose_join(*elements)
  options = (if elements.last.is_a?(Hash) then elements.pop else {} end)
  multi_line = options[:multi_line] || options[:m]
  padding = (multi_line ? /.*/m : /.*/)
  elements.inject(//) do |accumulator, element|
    accumulator + padding + element
  end
end

.to_plain_s(input, also_unescape = nil) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/quality_extensions/regexp/to_plain_s.rb', line 11

def self.to_plain_s(input, also_unescape = nil)
  input.
  gsub(/((?<!\\)(\\\\)*)\(((?=\?[^):]*:)\?[^):]*:)?/, '\1').   # remove all unescaped '('s and any '?-mix:b-style prefix that their contents begin with
  gsub(/((?<!\\)(\\\\)*)[)?*+\[\]]/, '\1').                    # remove all unescaped ')', '?', '*', etc.es
  gsub(/((?<!\\)(\\\\)*)\{.*\}/, '\1').                        # remove all unescaped /{.*}/
  gsub(/((?<!\\)(\\\\)*)\\([().\/#{also_unescape}])/, '\1\3').                    # unescape any remaining (escaped) '\(', '\)', '\.', etc.es
  gsub(/\\\\/, '\\')
end

Instance Method Details

#+(other) ⇒ Object

/a/ + /b/ == /ab/ Actually, the way it’s currently implemented, it is

/a/ + /b/ == /(?-mix:a)(?-mix:b)/

But they seem to be functionally equivalent despite the different spellings.



48
49
50
51
# File 'lib/quality_extensions/regexp/join.rb', line 48

def +(other)
  other = Regexp.escape(other) if other.is_a?(String)
  /#{self}#{other}/
end

#capture_namesObject



36
37
38
# File 'lib/quality_extensions/regexp/named_captures.rb', line 36

def capture_names
  @capture_names ||= extract_capture_names_from(source)
end

#debug_triple_equals(other) ⇒ Object Also known as: ===

not working



14
15
16
17
18
# File 'lib/quality_extensions/enumerable/grep_with_index.rb', line 14

def debug_triple_equals(other)
  p other if $debug
  #p original_tripel_equals(other)
  original_tripel_equals(other)
end

#eee_with_support_for_symbols(other) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/quality_extensions/symbol/match.rb', line 59

def eee_with_support_for_symbols (other)
  case other
  when Symbol
    __send__ :===, other.to_s
  else
    __send__ :===, other
  end
end

#match_with_named_captures(pattern) ⇒ Object



28
29
30
31
32
# File 'lib/quality_extensions/regexp/named_captures.rb', line 28

def match_with_named_captures(pattern)
  matchdata = match_without_named_captures(pattern)
  matchdata.capture_names = capture_names if matchdata.respond_to?(:capture_names=)
  matchdata
end

#to_plain_s(also_unescape = nil) ⇒ Object

Sometimes you don’t want to see a [serialized] version of the regular expression:

/a/.to_s       => '(?-mix:a)'

and you simply want a simple, human-readable version:

/a/.to_plain_s => 'a'

Since the Regexp object doesn’t know what it was delimited with, to_plain_s can’t know which delimiter character, if any, it should try to unescape.

%r#a#b#.to_plain_s(‘#’) # => “a#b” %r#a#b#.to_plain_s # => “a\#b”

An escaped ‘/’ will always be unescaped, since Ruby escapes for you even when you don’t:

/a\/b/.to_plain_s      # => 'a/b'

%r#a/b#.to_plain_s # => ‘a/b’ %r#a/b#. to_plain_s # => “a/b”



36
37
38
# File 'lib/quality_extensions/regexp/to_plain_s.rb', line 36

def to_plain_s(also_unescape = nil)
  Regexp.to_plain_s(self.to_s, also_unescape)
end