Skip to content

Instantly share code, notes, and snippets.

@adam12
Created August 28, 2021 12:27
Show Gist options
  • Save adam12/70f6304300928cc9cda02bbdf4e473bc to your computer and use it in GitHub Desktop.
Save adam12/70f6304300928cc9cda02bbdf4e473bc to your computer and use it in GitHub Desktop.
require "strscan"
class Scanner
def initialize(str, index)
start = str.rindex(/\b/, index)
@scanner = StringScanner.new(str)
@scanner.pos = start
end
def scan
word_1 = @scanner.scan(/\w+/)
@scanner.skip(/[\s\b]+/) # Ignore whitespace, comma, period, etc
word_2 = @scanner.scan(/\w+/)
"#{word_1} #{word_2}"
end
end
str = "peter is a jolly fellow, and my, clarice and him sure are a fit so mellow"
pp Scanner.new(str, Integer(ARGV[0]) || 12).scan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment