-
-
Save adam12/70f6304300928cc9cda02bbdf4e473bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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