Created
April 16, 2013 08:30
-
-
Save chsh/5394344 to your computer and use it in GitHub Desktop.
prex('ab_cd') generates %w(ab_cd ab_c abc a_cd a_c ac).
It's useful for prefix match without regular expression. :-)
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
# usage: prex('ab_cd') generates %w(ab_cd ab_c abc a_cd a_c ac). | |
# It's useful for prefix match without regular expression. :-) | |
def xpand(array) | |
first, *others = array | |
return first if others == [] | |
first.map do |it| | |
xpand(others).map do |jt| | |
[it + '_' + jt, it + jt] | |
end.flatten | |
end.flatten | |
end | |
def prex(word) | |
words = word.split('_') | |
prexes = words.map do |w| | |
len = w.length | |
(1..len).to_a.reverse.map { |i| w[0, i] } | |
end | |
xpand prexes | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment