Skip to content

Instantly share code, notes, and snippets.

@tadman
Created September 14, 2017 17:59
Show Gist options
  • Save tadman/638f7ee951238b25ebaab039ed11068e to your computer and use it in GitHub Desktop.
Save tadman/638f7ee951238b25ebaab039ed11068e to your computer and use it in GitHub Desktop.
class String
def substrings
((0...length).to_a.combination(2).map do |from,to|
# Extract substrings
self[from, to]
end + self.chars).map do |s|
# Trim off any unwanted characters
s.sub(/\A\W+/, '').sub(/\W+\z/, '')
end.sort.uniq.reject do |s|
# Remove empty strings
s.length == 0
end
end
def phrase_in_common(other)
[ self, other ].map(&:downcase).map(&:substrings).reduce(&:&).max_by(&:length)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment