Class: REXML::Source (Ruby 2.3.2)
Description: Class : REXML::Source - Ruby 2.3.2

- Home
- Core 2.3.2
- Std-lib 2.3.2
- Downloads

Support for the Ruby 2.3 series has ended. See here for reference.

### Home Classes Methods

### In Files

- rexml/source.rb

### Parent

Object

### Methods

- ::new
- #consume
- #current_line
- #empty?
- #encoding=
- #match
- #match_to
- #match_to_consume
- #position
- #read
- #scan

### Included Modules

- REXML::Encoding

### Class/Module Index

Quicksearch

- Array
- Fixnum
- Float
- Object
- REXML
- REXML::AttlistDecl
- REXML::Attribute
- REXML::Attributes
- REXML::CData
- REXML::Child
- REXML::Comment
- REXML::DTD
- REXML::DTD::AttlistDecl
- REXML::DTD::ElementDecl
- REXML::DTD::EntityDecl
- REXML::DTD::NotationDecl
- REXML::DTD::Parser
- REXML::Declaration
- REXML::DocType
- REXML::Document
- REXML::Element
- REXML::ElementDecl
- REXML::Elements
- REXML::Encoding
- REXML::Entity
- REXML::EntityConst
- REXML::ExternalEntity
- REXML::Formatters
- REXML::Formatters::Default
- REXML::Formatters::Pretty
- REXML::Formatters::Transitive
- REXML::Functions
- REXML::IOSource
- REXML::Instruction
- REXML::Light
- REXML::Light::Node
- REXML::Namespace
- REXML::Node
- REXML::NotationDecl
- REXML::Output
- REXML::Parent
- REXML::ParseException
- REXML::Parsers
- REXML::Parsers::BaseParser
- REXML::Parsers::LightParser
- REXML::Parsers::PullEvent
- REXML::Parsers::PullParser
- REXML::Parsers::SAX2Parser
- REXML::Parsers::StreamParser
- REXML::Parsers::TreeParser
- REXML::Parsers::UltraLightParser
- REXML::Parsers::XPathParser
- REXML::QuickPath
- REXML::SAX2Listener
- REXML::Security
- REXML::Source
- REXML::SourceFactory
- REXML::StreamListener
- REXML::SyncEnumerator
- REXML::Text
- REXML::UndefinedNamespaceException
- REXML::Validation
- REXML::Validation::Choice
- REXML::Validation::Event
- REXML::Validation::Interleave
- REXML::Validation::OneOrMore
- REXML::Validation::Optional
- REXML::Validation::Ref
- REXML::Validation::RelaxNG
- REXML::Validation::Sequence
- REXML::Validation::State
- REXML::Validation::ValidationException
- REXML::Validation::Validator
- REXML::Validation::ZeroOrMore
- REXML::XMLDecl
- REXML::XMLTokens
- REXML::XPath
- REXML::XPathParser
- Symbol

No matching classes.

# REXML::Source

A Source can be searched for patterns, and wraps buffers and other objects and provides consumption of text

### Attributes

buffer[R]

The current buffer (what we're going to read next)

encoding[R]
line[R]

The line number of the last consumed text

### Public Class Methods

new(arg, encoding=nil) click to toggle source

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
        detect_encoding
      end
      @line = 0
    end

### Public Instance Methods

consume( pattern ) click to toggle source

                   # File rexml/source.rb, line 85
    def consume( pattern )
      @buffer = $' if pattern.match( @buffer )
    end

current_line() click to toggle source

@return the current line in the source

                   # File rexml/source.rb, line 115
    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

empty?() click to toggle source

@return true if the Source is exhausted

                   # File rexml/source.rb, line 106
    def empty?
      @buffer == ""
    end

encoding=(enc) click to toggle source

Inherited from Encoding Overridden to support optimized en/decoding

                   # File rexml/source.rb, line 54
    def encoding=(enc)
      return unless super
      encoding_updated
    end

match(pattern, cons=false) click to toggle source

                   # File rexml/source.rb, line 99
    def match(pattern, cons=false)
      md = pattern.match(@buffer)
      @buffer = $' if cons and md
      return md
    end

match_to( char, pattern ) click to toggle source

                   # File rexml/source.rb, line 89
    def match_to( char, pattern )
      return pattern.match(@buffer)
    end

match_to_consume( char, pattern ) click to toggle source

                   # File rexml/source.rb, line 93
    def match_to_consume( char, pattern )
      md = pattern.match(@buffer)
      @buffer = $'
      return md
    end

position() click to toggle source

                   # File rexml/source.rb, line 110
    def position
      @orig.index( @buffer )
    end

read() click to toggle source

                   # File rexml/source.rb, line 82
    def read
    end

scan(pattern, cons=false) click to toggle source

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 regexps 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 75
    def scan(pattern, cons=false)
      return nil if @buffer.nil?
      rv = @buffer.scan(pattern)
      @buffer = $' if cons and rv.size>0
      rv
    end