Class: Walrat::StringEnumerator
- Inherits:
-
Object
- Object
- Walrat::StringEnumerator
- 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
-
#last ⇒ Object
readonly
Returns the char most recently scanned before the last “next” call, or nil if nothing previously scanned.
Instance Method Summary collapse
-
#initialize(string) ⇒ StringEnumerator
constructor
A new instance of StringEnumerator.
-
#next ⇒ Object
This method will only work as expected if $KCODE is set to ‘U’ (UTF-8).
Constructor Details
#initialize(string) ⇒ StringEnumerator
Returns a new instance of StringEnumerator.
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
#last ⇒ Object (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
#next ⇒ Object
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 |