Class: Mustermann::StringScanner::ScanResult

Inherits:
String
  • Object
show all
Defined in:
lib/mustermann/string_scanner.rb

Overview

Encapsulates return values for #scan, #check, and friends. Behaves like a String (the substring which matched the pattern), but also exposes its position in the main string and any params parsed from it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scanner, position, length, params = {}) ⇒ ScanResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ScanResult.



98
99
100
# File 'lib/mustermann/string_scanner.rb', line 98

def initialize(scanner, position, length, params = {})
  @scanner, @position, @length, @params = scanner, position, length, params
end

Instance Attribute Details

#lengthInteger (readonly)

Returns length of the substring.

Examples:

require 'mustermann/string_scanner'
scanner = Mustermann::StringScanner.new('foo/bar')
scanner.scan(:name).length # => 3
scanner.getch.length       # => 1
scanner.scan(:name).length # => 3

Returns:

  • (Integer)

    length of the substring



79
80
81
# File 'lib/mustermann/string_scanner.rb', line 79

def length
  @length
end

#paramsHash (readonly)

Params parsed from the substring. Will not include params from previous scan results.

Examples:

require 'mustermann/string_scanner'
scanner = Mustermann::StringScanner.new('foo/bar')
scanner.scan(:name).params # => { "name" => "foo" }
scanner.getch.params       # => {}
scanner.scan(:name).params # => { "name" => "bar" }

Returns:

  • (Hash)

    params parsed from the substring

See Also:



95
96
97
# File 'lib/mustermann/string_scanner.rb', line 95

def params
  @params
end

#positionInteger (readonly) Also known as: pos

Returns position the substring starts at.

Examples:

require 'mustermann/string_scanner'
scanner = Mustermann::StringScanner.new('foo/bar')
scanner.scan(:name).position # => 0
scanner.getch.position       # => 3
scanner.scan(:name).position # => 4

Returns:

  • (Integer)

    position the substring starts at



68
69
70
# File 'lib/mustermann/string_scanner.rb', line 68

def position
  @position
end

#scannerObject (readonly)

The scanner this result came from.

Examples:

require 'mustermann/string_scanner'
scanner = Mustermann::StringScanner.new('foo/bar')
scanner.scan(:name).scanner == scanner # => true


58
59
60
# File 'lib/mustermann/string_scanner.rb', line 58

def scanner
  @scanner
end