Method: StringScanner#each

Defined in:
lib/rmtools/text/string_scanner.rb

#each(re, cbs = nil, &cb) ⇒ Object

#each( <Regexp>, { <0..255 | :~ | nil> => ->{|self|}, … } ) #each( <Regexp>, [ [ <Regexp>, ->{|self, <MatchData>|} ], … ] ) #each( <Regexp> ) {|self|} Example:

ss = StringScanner.new xpath
ss.each %r{\[-?\d+\]|\{[^\}]+\}}, 
  ?[ => lambda {|ss| 
    if node; node = FindByIndex[node, nslist, ss]
    else     return []     end  }, 
  ?{ => lambda {|ss| 
    if node; node = FindByProc[node, nslist, ss]
    else     return []     end  },
  nil => lambda {|str|
    node = node.is(Array) ?
      node.sum {|n| n.__find(str, nslist).to_a} : node.__find(str, nslist) 
  }


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rmtools/text/string_scanner.rb', line 24

def each(re, cbs=nil, &cb)
  @last = pos
  res = scan_until re
  if cbs
    if cbs.is Hash
      while res
        if cb = cbs[matched[0]] || cbs[:~]
          cb[self]
          @last = pos
          res = !eos? && scan_until(re)
        else break
        end
      end
      if !eos? and cb = cbs[nil]
        cb[tail]
      end
    else
      while res
        if cb = cbs.find {|pattern, fun| pattern and matched[pattern]}
          # patterns should be as explicit as possible
          cb[1][self, $~] if cb[1]
          @last = pos
          res = !eos? && scan_until(re)
        else break
        end
      end
      if !eos? and cb = cbs.find {|pair| pair[0].nil?}
        cb[1][tail]
      end
    end
  else
    while res
      cb[self]
      @last = pos
      res = !eos? && scan_until(re)
    end
  end
end