Extended maintenance of Ruby 1.9.3 ended on February 23, 2015. Read more
A Source can be searched for patterns, and wraps buffers and other objects and provides consumption of text
Constructor @param arg must be a String, and should be a valid XML document @param encoding if non-null, sets the encoding of the source to this value, overriding all encoding detection
# File rexml/source.rb, line 41
def initialize(arg, encoding=nil)
@orig = @buffer = arg
if encoding
self.encoding = encoding
else
self.encoding = check_encoding( @buffer )
end
@line = 0
end
# File rexml/source.rb, line 92
def consume( pattern )
@buffer = $' if pattern.match( @buffer )
end
@return the current line in the source
# File rexml/source.rb, line 122
def current_line
lines = @orig.split
res = lines.grep @buffer[0..30]
res = res[-1] if res.kind_of? Array
lines.index( res ) if res
end
@return true if the Source is exhausted
# File rexml/source.rb, line 113
def empty?
@buffer == ""
end
Inherited from Encoding Overridden to support optimized en/decoding
# File rexml/source.rb, line 54
def encoding=(enc)
return unless super
@line_break = encode( '>' )
if @encoding != 'UTF-8'
@buffer = decode(@buffer)
@to_utf = true
else
@to_utf = false
@buffer.force_encoding ::Encoding::UTF_8
end
end
# File rexml/source.rb, line 106
def match(pattern, cons=false)
md = pattern.match(@buffer)
@buffer = $' if cons and md
return md
end
# File rexml/source.rb, line 96
def match_to( char, pattern )
return pattern.match(@buffer)
end
# File rexml/source.rb, line 100
def match_to_consume( char, pattern )
md = pattern.match(@buffer)
@buffer = $'
return md
end
# File rexml/source.rb, line 117
def position
@orig.index( @buffer )
end
Scans the source for a given pattern. Note, that this is not your usual scan() method. For one thing, the pattern argument has some requirements; for another, the source can be consumed. You can easily confuse this method. Originally, the patterns were easier to construct and this method more robust, because this method generated search regexes on the fly; however, this was computationally expensive and slowed down the entire REXML package considerably, since this is by far the most commonly called method. @param pattern must be a Regexp, and must be in the form of /^s*(#{your pattern, with no groups})(.*)/. The first group will be returned; the second group is used if the consume flag is set. @param consume if true, the pattern returned will be consumed, leaving everything after it in the Source. @return the pattern, if found, or nil if the Source is empty or the pattern is not found.
# File rexml/source.rb, line 82
def scan(pattern, cons=false)
return nil if @buffer.nil?
rv = @buffer.scan(pattern)
@buffer = $' if cons and rv.size>0
rv
end
Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.
If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.