Skip to content

Instantly share code, notes, and snippets.

@chsh
Created April 16, 2013 08:30
Show Gist options
  • Save chsh/5394344 to your computer and use it in GitHub Desktop.
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. :-)
# 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