Skip to content

Instantly share code, notes, and snippets.

@snicky
Last active March 15, 2016 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snicky/12148253fc23af5e1c38 to your computer and use it in GitHub Desktop.
Save snicky/12148253fc23af5e1c38 to your computer and use it in GitHub Desktop.
class String
def constitutes_of?(phrases)
_constitutes_of?(phrases.sort_by!(&:length))
end
protected
def _constitutes_of?(phrases)
return false if phrases.empty?
absent_phrases = []
phrases.each do |phrase|
remainders = []
if (i = index(phrase))
remainders << self[0..i-1] if i > 0
remainders << self[i+phrase.length..-1]
else
absent_phrases << phrase
next
end
remainders.sort_by!(&:length)
phrases_left = phrases - absent_phrases
return true if remainders.all? { |r| r.empty? || r._constitutes_of?(phrases_left) }
end
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment