Class: Walrat::StringEnumerator

Inherits:
Object
  • Object
show all
Defined in:
lib/walrat/string_enumerator.rb

Overview

Unicode-aware (UTF-8) string enumerator. For Unicode support $KCODE must be set to ‘U’ (UTF-8).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ StringEnumerator

Returns a new instance of StringEnumerator.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
# File 'lib/walrat/string_enumerator.rb', line 34

def initialize string
  raise ArgumentError, 'nil string' if string.nil?
  @scanner  = StringScanner.new string
  @current  = nil
  @last     = nil
end

Instance Attribute Details

#lastObject (readonly)

Returns the char most recently scanned before the last “next” call, or nil if nothing previously scanned.



32
33
34
# File 'lib/walrat/string_enumerator.rb', line 32

def last
  @last
end

Instance Method Details

#nextObject

This method will only work as expected if $KCODE is set to ‘U’ (UTF-8).



42
43
44
45
# File 'lib/walrat/string_enumerator.rb', line 42

def next
  @last     = @current
  @current  = @scanner.scan(/./m) # must use multiline mode or "." won't match newlines
end