This is probably not the most concise solution (depends if you already have a library for this), but one elegant method is to use a trie. I use tries for implementing tab completion in my Scheme interpreter:
[
[To see links please register here]
][1]
For example:
tree = Trie.new
%w[interspecies interstelar interstate].each { |s| tree[s] = true }
tree.longest_prefix('')
#=> "inters"
I also use them for matching channel names with wildcards for the Bayeux protocol; see these:
[
[To see links please register here]
][2]
[
[To see links please register here]
][3]
[1]:
[To see links please register here]
[2]:
[To see links please register here]
[3]:
[To see links please register here]