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.



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

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



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

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:



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

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



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

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


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

def scanner
  @scanner
end