Skip to content

Instantly share code, notes, and snippets.

@sixtyfive
Forked from adam12/irc-aug-28.rb
Last active August 29, 2021 15:31
Show Gist options
  • Save sixtyfive/03b90ccac4ec7556dc8859e5f55f8cdf to your computer and use it in GitHub Desktop.
Save sixtyfive/03b90ccac4ec7556dc8859e5f55f8cdf to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
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"
pos = ARGV[0] ? ARGV[0].to_i : 12
print "\n #{str}\n #{' '*pos}^\n\n=> "
pp Scanner.new(str, pos).scan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment