Type:
Class

StringScanner provides for lexical scanning operations on a String. Here is an example of its usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
s = StringScanner.new('This is an example string')
s.eos?               # -> false
 
p s.scan(/\w+/)      # -> "This"
p s.scan(/\w+/)      # -> nil
p s.scan(/\s+/)      # -> " "
p s.scan(/\s+/)      # -> nil
p s.scan(/\w+/)      # -> "is"
s.eos?               # -> false
 
p s.scan(/\s+/)      # -> " "
p s.scan(/\w+/)      # -> "an"
p s.scan(/\s+/)      # -> " "
p s.scan(/\w+/)      # -> "example"
p s.scan(/\s+/)      # -> " "
p s.scan(/\w+/)      # -> "string"
s.eos?               # -> true
 
p s.scan(/\s+/)      # -> nil
p s.scan(/\w+/)      # -> nil

Scanning a string means remembering the position of a scan pointer, which is just an index. The point of scanning is to move forward a bit at a time, so matches are sought after the scan pointer; usually immediately after it.

Given the string “test string”, here are the pertinent scan pointer positions:

1
2
3
  t e s t   s t r i n g
0 1 2 ...             1
                      0

When you scan for a pattern (a regular expression), the match must occur at the character after the scan pointer. If you use scan_until, then the match can occur anywhere after the scan pointer. In both cases, the scan pointer moves just beyond the last character of the match, ready to scan again from the next character onwards. This is demonstrated by the example above.

Method Categories

There are other methods besides the plain scanners. You can look ahead in the string without actually scanning. You can access the most recent match. You can modify the string being scanned, reset or terminate the scanner, find out or change the position of the scan pointer, skip ahead, and so on.

Advancing the Scan Pointer

Looking Ahead

Finding Where we Are

Setting Where we Are

Match Data

Miscellaneous

There are aliases to several of the methods.

pointer
  • References/Ruby on Rails/Ruby/Classes/StringScanner

pointer() Instance Public methods Returns the byte position of the scan pointer

2025-01-10 15:47:30
skip
  • References/Ruby on Rails/Ruby/Classes/StringScanner

skip(pattern) Instance Public methods Attempts to skip over the given pattern

2025-01-10 15:47:30
matched
  • References/Ruby on Rails/Ruby/Classes/StringScanner

matched() Instance Public methods Returns the last matched string.

2025-01-10 15:47:30
scan_until
  • References/Ruby on Rails/Ruby/Classes/StringScanner

scan_until(pattern) Instance Public methods Scans the string until

2025-01-10 15:47:30
search_full
  • References/Ruby on Rails/Ruby/Classes/StringScanner

search_full(pattern, advance_pointer_p, return_string_p) Instance Public methods Scans

2025-01-10 15:47:30
beginning_of_line?
  • References/Ruby on Rails/Ruby/Classes/StringScanner

beginning_of_line?() Instance Public methods Returns true iff the

2025-01-10 15:47:30
post_match
  • References/Ruby on Rails/Ruby/Classes/StringScanner

post_match() Instance Public methods Return the post-match

2025-01-10 15:47:30
terminate
  • References/Ruby on Rails/Ruby/Classes/StringScanner

terminateclear Instance Public methods

2025-01-10 15:47:30
rest_size
  • References/Ruby on Rails/Ruby/Classes/StringScanner

rest_size() Instance Public methods s.rest_size is equivalent to

2025-01-10 15:47:30
clear
  • References/Ruby on Rails/Ruby/Classes/StringScanner

clear() Instance Public methods Equivalent to

2025-01-10 15:47:30