Class: Regexp

Inherits:
Object show all
Defined in:
lib/modalsupport/regexp.rb

Instance Method Summary collapse

Instance Method Details

#match_all(str) ⇒ Object

Pass each match to a block: string.match_all{|match_data| …}



4
5
6
7
8
9
10
11
12
13
# File 'lib/modalsupport/regexp.rb', line 4

def match_all(str)
  result = []
  i = 0
  while r=str.index(self, i)
    match = Regexp.last_match
    i = r + match.to_s.length
    result << yield(match)
  end
  result
end

#match_one(str, i = 0) ⇒ Object

Pass the first match to a block: string.match_one{|match_data| …} Similar to Ruby 1.9 match, but does not set the special varialbes $~, $1, etc. in the caller’s space.



17
18
19
20
21
22
23
24
25
# File 'lib/modalsupport/regexp.rb', line 17

def match_one(str, i=0)
  str = str[i..-1] if i>0
  m = self.match(str)
  if m && block_given?
    yield m
  else
    m
  end
end