Skip to content

Instantly share code, notes, and snippets.

@shugo
Created May 23, 2024 02:15
Show Gist options
  • Save shugo/cf40c4a2909430675523fe3bc405b13e to your computer and use it in GitHub Desktop.
Save shugo/cf40c4a2909430675523fe3bc405b13e to your computer and use it in GitHub Desktop.
Pure Ruby StringScanner
class StringScanner
attr_reader :string
def initialize(string, *args)
@string = string
@pos = 0
@last_match = nil
end
def scan(re)
if @string.byteindex(/\G#{re}/, @pos)
@pos = $~.byteoffset(0)[1]
end
@last_match = $~
$&
end
def method_missing(mid, ...)
@last_match.send(mid, ...)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment