Class: Regexp

Inherits:
Object show all
Defined in:
lib/qualitysmith_extensions/symbol/match.rb,
lib/qualitysmith_extensions/regexp/join.rb

Overview

I can’t even run the tests in this file after making this (trivial) wrapper for ===! If I try, I get this error: /usr/lib/ruby/1.8/optparse.rb:1099:in ‘make_switch’: undefined method ‘downcase’ for nil:NilClass (NoMethodError)

from /usr/lib/ruby/1.8/optparse.rb:1032:in `each'
from /usr/lib/ruby/1.8/optparse.rb:1032:in `make_switch'
from /usr/lib/ruby/1.8/optparse.rb:1140:in `define'
from /usr/lib/ruby/1.8/optparse.rb:1149:in `on'
from /usr/lib/ruby/1.8/test/unit/autorunner.rb:106:in `options'
from /usr/lib/ruby/1.8/optparse.rb:755:in `initialize'
from /usr/lib/ruby/1.8/test/unit/autorunner.rb:101:in `new'
from /usr/lib/ruby/1.8/test/unit/autorunner.rb:101:in `options'
from /usr/lib/ruby/1.8/test/unit/autorunner.rb:88:in `process_args'
from /usr/lib/ruby/1.8/test/unit/autorunner.rb:10:in `run'
from /usr/lib/ruby/1.8/test/unit.rb:278
from -:46

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/qualitysmith_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/qualitysmith_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

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/qualitysmith_extensions/regexp/join.rb', line 48

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

#eee_with_support_for_symbols(other) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/qualitysmith_extensions/symbol/match.rb', line 61

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